String


String

I have this large quantity of string, a hundred and twenty-two thousand miles of it to be exact,…

We have already met strings indirectly throughout some of the other tutorials.  If you’ll recall, strings are the things you get when you put inverted commas around stuff – like this: ‘stuff’.   As far as Python is concerned, the inverted commas transform what might be other sorts of data into a string.  There are actually two three four Amongst our ways of making a string are such diverse elements as: using inverted commas (‘), using quotation marks (“) , using triple inverted commas (”’) [update:] and triple quotes (“””).  Which sort you use depends largely on your preference.  Sometimes, you need to use a certain manner of creating the string because the string itself contains (eg) an inverted comma.

>>> ‘a string’
‘a string’
>>> ‘This isn’t really a string because the inverted comma in the middle cuts the string off early.’
File “<stdin>”, line 1
‘This isn’t really a string because the inverted comma in the middle cuts the string off early.’
^
SyntaxError: invalid syntax
>>> “However, you can use quotation marks when there’s a need to include an inverted comma in a string”
“However, you can use quotation marks when there’s a need to include an inverted comma in a string”
>>> ‘Conversely, if you need to use “quotation marks” in a string, you can make the string using inverted commas’
‘Conversely, if you need to use “quotation marks” in a string, you can make the string using inverted commas’
>>> ”’Finally, you can pretty much always use triple inverted commas.  When you do, you don’t have to worry about whether there are “quotation marks” in the string or inverted commas.”’
‘Finally, you can pretty much always use triple inverted commas.  When you do, you don\’t have to worry about whether there are “quotation marks” in the string or inverted commas.’

Can you see, in the last string, Python has represented the ‘ by using \’?  This is called ‘escaping’ a character.  Some escaped characters have specific meanings.  The ones which will see most often are: \t which means tab, and \n which means new line:

>>> print '1\t2\n3'
1       2
3

When Python printed the string it didn’t actually print ‘\t’ it actually interpreted it as a control character and put a tab space between the 1  and the 2 (and a new line between the 2 and the 3).

Cut String

Mr. Simpson:     Ah, but there’s a snag, you see. Due to bad planning, the hundred and twenty-two thousand miles is in three inch lengths. So it’s not very useful.
Wapcaplet:     Well, that’s our selling point! “SIMPSON’S INDIVIDUAL STRINGETTES!”

Sometimes  you don’t want the whole of the string.  Sometimes you just want a bit of it.   Python comes with a number of ways to chop up strings (and to put them back together again).  The chop function (actually a method, but that’s for another time) is called .split() and the function for putting them together is called .join() (if it flows off the side where you can’t see it you can still copy and paste it – sorry):

>>> simpson = '''the hundred and twenty-two thousand miles is in three inch lengths'''
>>> print simpson
the hundred and twenty-two thousand miles is in three inch lengths
>>> simpsonSplit = simpson.split(' ')
>>> print simpsonSplit
['the', 'hundred', 'and', 'twenty-two', 'thousand', 'miles', 'is', 'in', 'three', 'inch', 'lengths']
>>> print ' '.join(simpsonSplit)
the hundred and twenty-two thousand miles is in three inch lengths

Can you see that .split(‘ ‘) seems to have ‘split’ the string (it’s actually still there, the ‘pieces’ are in a separate list object) .  It split it wherever there was a blank space (‘ ‘).  We could have easily split it on a different (sub-)string, like the letter ‘e’:

>>> print simpson.split('e')
['th', ' hundr', 'd and tw', 'nty-two thousand mil', 's is in thr', '', ' inch l', 'ngths']

You should also note that: there is a dot . at the front of .split(); its usage is someString.split(‘someOtherString’); and after .split() split the string, we had a list of strings. Unlike poor Mr Simpson, if our strings are chopped up into little pieces, we can .join() them back together again.  The structure of .join() is the reverse of .split().  At the front of .join() you put the thing you want to join the strings with.  In the brackets you put a list that you want to join up (it must be a list). As with .split() you can put any string as the joining string.  See what you get from:

>>> print '\n'.join(simpson.split(' '))

You can .split() and .join() things in chains if you really want to, to make some 133t speak:

>>> '1'.join(('4'.join('3'.join(simpson.split('e')).split('a')).split('l')))

‘th3 hundr3d 4nd tw3nty-two thous4nd mi13s is in thr33 inch 13ngths’

Due to Bad Planning

You can also slice strings up, but without actually cutting them.  You do this by using the [:] operator.  It takes up to two number (actually int)  arguments. Instead of explaining I will give you some examples:  (the comments after <- I have typed in after don’t expect to see them in your output)

>>> aString = '0123456789ABCDEFGHIJ'
>>> aString[0]
'0'  <- get the first character (number 0) in the string
>>> aString[1]
'1' <- get the second character (number 1
       - yes, 1 just believe on this) in the string
>>> aString[10]
'A' <- get 11th character
>>> aString[1:1]
''  <- get the characters from the second character up to,
       but not including the second character - ie empty
>>> aString[1:2]
'1' <- starting on the second up to
       but not including the third (ie number 2)
>>> aString[1:3]
'12' <- starting on the second utbni the fourth (number 3)
>>> aString[1:10]
'123456789' <- starting on the second utbni the 11th
>>> aString[:10]
'0123456789' <- starting from the start of the string
                utbni the 11th
             <- note there are 10 numbers in the string
             <- note also nothing in front of the colon
>>> aString[10:]
'ABCDEFGHIJ' <- starting from the 11th to the end of the string.
             <- nothing after the colon
>>> aString[9:]
'9ABCDEFGHIJ'<- starting from the 10th to the end of the string
>>> aString[-1]
'J' <- last character putting a - before the number says start
       from the end of the string and work back
>>> aString[-4:]
'GHIJ' <- last 4 characters
>>> aString[:-10]
'0123456789' <- starting from the start up to but not including
                the 10th character from the end
>>> aString[:-11]
'012345678' <- utbni the 11th character from the end
>>> aString
'0123456789ABCDEFGHIJ'  <- the string has not changed by doing
                           any of this
>>> start=5
>>> end=12
>>> aString[start:end]
'56789AB'   <- you can even use variables around the colon

HomeWork:

Change this for loop so that it separately prints each character in aString above:

>>> for i in range(20):
...     print i,': '+ aString[10]

Change the following to store the name of your school in the variable a.  Then use aInBits = a.split() to break it on the ‘o’s, and .join(aInBits) to join it up with zeroes where the ‘o’s where. You need to work out what goes in the brackets  of a.split() and what goes in front of .join(aInBits) hint: it’s a string and it has zero in it:

a='Baloney Public School'

Extra (or if there were no os in your school’s name):

store what you’ve done and replace the ‘e’s with ’3′s, the ‘a’s with ’4′s, the ‘S’s with ‘$’s and the ‘b’s with ’6′s (you can recycle the variable aInBits)


About these ads

12 Responses to String

  1. Ghislain Hivon says:

    Hi,

    I hope that one day my daughter would like me to teach her some python.

    There a fourth way to declare a string :
    “””triple quotation marks also work”””

    “””Also with triple quotation marks or triple inverted comma
    you can use newline in the string
    And they will be preserve”””
    “””Also with triple quotation marks or triple inverted comma \nyou can use newline in the string\nAnd they will be preserve”””

  2. brendanscott says:

    I’ve never tried triple quotes before. Thanks for the tip.

  3. Pingback: Links 6/8/2010: The Linux Desktop More Responsive, Android Succeeds on Smartphones | Techrights

  4. Pingback: Consolidation: CryptoPyThy « Python Tutorials for Kids 8+

  5. Ryan says:

    Hi! My name is Ryan, I’m a kid who would like to learn Python.

    I’ve got the basics down, but I’m having some trouble with putting raw_input in a string. Do you understand what I am saying?

    Here’s my code so far:
    print “””
    Hi! My name is Joey!!! What’s your name???
    “””
    #gets input from user
    name = raw_input(“Tell Joey your name.”)

    I want to put “name” into a string that goes:

    “Oh, so your name is “name””

    Are you getting what I’m saying?

    Please help me. Thanks.

    Ryan

  6. brendanscott says:

    All in the syntax Ryan:

    >>> name = raw_input(‘Tell me your name: ‘)
    Tell me your name: Python4Kids
    >>> print ‘Oh, so your name is ‘,name,’?’
    Oh, so your name is Python4Kids ?
    >>> print ‘Oh, so your name is ‘+name+’?’
    Oh, so your name is Python4Kids?
    >>>

    The structure is:
    print [message surrounded by inverted commas] [a comma or a plus sign] [the variable]

    The comma puts more space between the message and the variable than the plus does (see the output).

    Once you’ve done the raw_input whatever was typed is stored (as a string) in the variable called name. Not ‘name’ (the inverted commas would make it a string):

    >>> print name
    Python4Kids
    >>> print ‘name’
    name
    >>>

    See the difference?

    Another way to do it:
    >>> message = ‘I think your name is ‘+name
    >>> print message
    I think your name is Python4Kids
    >>> message2 = ‘I think your name is “‘+name+’”‘
    >>> print message2
    I think your name is “Python4Kids”
    >>>

  7. Pingback: Trivia Game (Part 2) « Python Tutorials for Kids 8+

  8. Bezz says:

    I’m having a hard time figuring out this day’s homework. Can I get the solution please?

  9. Pingback: Really GUI (more on Buttons, images and handlers) « Python Tutorials for Kids 8+

  10. moskva1300 says:

    On extra part if I understood its good if in a list there isnt any o and s,right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: