Python Strings

Main.PythonStrings History

Hide minor edits - Show changes to markup

April 24, 2007, at 09:25 AM EST by jza -
Changed lines 75-82 from:

The opposite of split() will be the 'join() method which will join different elements. This is most commonly used on the Python Lists page.

to:

The opposite of split() will be the 'join() method which will join different elements. This is most commonly used on the Python Lists page.

>>> lst = ['here','is','a','list','of','words']
>>> print '-+-'.join(lst)
here-+-is-+-a-+-list-+-of-+-words
>>> print ' '.join(lst)
here is a list of words
April 24, 2007, at 09:20 AM EST by jza -
Deleted lines 45-47:

Methods

Changed lines 60-61 from:

'r' @]

to:

'r' @]

Methods

We can modify the string by using methods, one of this methods would be to capitalize() or to make all uppercase we can call on upper() method.

>>> print variable.capitalize()
String
>>> print variable.upper()
STRING
>>> print variable.center() 

Similar to slice, we can use split() method however split will go on parsing the string based on the white spaces. So if the value have spaces split will come through.

>>> sentence = "My name is Earl"
>>> sentence.split()
['My', 'name', 'is', 'Earl'] 

The opposite of split() will be the 'join() method which will join different elements. This is most commonly used on the Python Lists page.

April 24, 2007, at 09:10 AM EST by jza -
Changed lines 1-2 from:
  • Recording on Strings
to:
  • Recording on Strings (ogg file 12:26)
Deleted lines 8-10:
Changed lines 34-36 from:

Methods

To take out different pieces or access individual elements we apply the slice notation. We can define as subsets of the sequence. This notation is a bracket and establish a minimum to a maximum of elements which we want to take out.

to:

We can know the number of elements using the len() function.

Added lines 36-50:

>>> len(variable) 6 >>> len(variable[0:3]) 3 >>> len(number) Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: object of type 'int' has no len() >>> len(str(number)) 2@]

Methods

To take out different pieces or access individual elements we apply the slice notation. We can define as subsets of the sequence. This notation is a bracket and establish a minimum to a maximum of elements which we want to take out. [@

Changed lines 55-64 from:

@]

to:

@] Python is a 0 offset language that means that it start from 0 instead of 1. Strings are a group of elements, so the first element will be the 0 element. So in a 10 element string the first will be 0 and the last will be 9. So we can point to the index we want based on this sequence:

>>> variable[0]
's'
>>> variable[1]
't'
>>> variable[2]
'r'
April 24, 2007, at 08:58 AM EST by jza -
Changed lines 29-30 from:

Functions that applies to it

to:

Functions that applies to strings

Added lines 37-38:

Methods

Changed lines 45-47 from:

@]

Methods

to:

@]

April 24, 2007, at 08:57 AM EST by jza -
Changed line 16 from:

>>> variable = "string" Strings is everything between double quotes regardless of what it has

to:

>>> variable = "string" Strings is everything between double quotes regardless of what it has

Changed line 19 from:

>>> print variable print is an external method give us the string representation

to:

>>> print variable print is an external method give us the string representation

Changed line 26 from:

>>> print number We get a string representation of the integer

to:

>>> print number We get a string representation of the integer

Changed line 31 from:

We can convert an integer to a string using the str() function. This takes what was a number and make it a string.

to:

We can convert an integer to a string using the str() function. This takes what was a number and make it a string.

Changed line 33 from:

>>> str(number) Using the str() function we change the numerical value into a string

to:

>>> str(number) Using the str() function we change the numerical value into a string

Added lines 37-44:

To take out different pieces or access individual elements we apply the slice notation. We can define as subsets of the sequence. This notation is a bracket and establish a minimum to a maximum of elements which we want to take out.

>>> variable[0:1]
's'
>>> variable[2:100]
'ring'
April 24, 2007, at 08:47 AM EST by jza -
Changed lines 1-37 from:
  • Recording on Strings
to:
  • Recording on Strings

Strings is a sequence type, sequence collection or group of things together.

Key characteristics

Strings can be numerical or alphanumerical, and also they are immutable. Immutable means that once you create a string you can't change it. Strings are homogeneous, which means that they can only take alphanumeric characters.

What to use it for

We use string to hold information.

>>> variable = "string"  ''' Strings is everything between double quotes regardless of what it has '''
>>> variable
'string'
>>> print variable      ''' print is an external method give us the string representation'''
string

Now we play with integers

>>> number=23
>>> print number        ''' We get a string representation of the integer'''
23 

Functions that applies to it

We can convert an integer to a string using the str() function. This takes what was a number and make it a string.

>>> str(number)         ''' Using the str() function we change the numerical value into a string'''
'23'

Methods

April 24, 2007, at 08:27 AM EST by jza -
Added line 1:
  • Recording on Strings