Saturday, March 3, 2012

Strings in Python

In this post we will how strings are used in Pythons

As usual we will open up the IDLE and let type some strings like


>>> "Welcome Reader"
'Welcome Reader'
>>> 'Welcome Reader'
'Welcome Reader'

We see here that strings are written inside double quotes("") or single quotes(''), either ways Python assumes that anything inside single quotes or double quotes are String type data

But here is a problem, how do we write a string which would display something like

Subir says "One day you will speak Python well"

Lets just try to print it and check

>>> "Subir says "One day you will speak Python well""
SyntaxError: invalid syntax

So here is the problem, we did follow the basic rule that anything we want to display for strings we have to use quotes, but we did it right?

Still it gave a Syntax Error

Here is the explanation:

Python looked it like, the double quote started before the word Subir, and it came across another double quotes before the word One, well that's good, now, when python looks at the next words on the line and thinks what the hell he is trying me to display, i don't know what is the meaning for One day blah blah blah, and finally at the end quotes again?? So let me throw a Syntax Error at him, because this is not how I work.

Well, Python went rude here, so what we do here to make Python understand is, we can use a backslash (\), In Python, the next character after a backslash is meaningless, Python thinks, okay the user wanted to me ignore whatever is there as the first character after backslash, and wanted me to simply print for the user.

So here is how we type it again:

>>> "Subir says \"One day you will speak Python well\""
'Subir says "One day you will speak Python well"'
>>> print "Subir says \"One day you will speak Python well\""
Subir says "One day you will speak Python well"
>>>

So Python gave us what we now wanted him to print out

We can store strings in a variable, lets check it out, how it works:

>>>
>>> a = "Welcome "
>>> b = "Visitor"
>>> a + b
'Welcome Visitor'
>>

Here Welcome was assigned to variable a, and Visitor was assigned to variable b, and like most of the other programming language, to concatenate strings(adding strings), we use the + operator

So when we typed a + b, it joined the values of the variable and displayed as 'Welcome Visitor'

So now we know how to concatenate Strings in Python

Okay, now what if we want to concatenate a number to a string, lets check it out:

>>> number = 1
>>> print "If you are not reading this post from the beginning, you wont understand anything, please go to post number " + number

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    print "If you are not reading this post from the beginning, you wont understand anything, please go to post number" + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>>

Here, Python gave an error, because concatenation is actually done between two same type of data, either integer or string, python doesn't understand normally, and will give us an error, if we try to concatenate two different types of data.

So here is the work around for this, because we want this to be displayed.

And the work around is, we use the function str, here is how it will be,

>>> number = str(1)
>>> print "If you are not reading this post from the beginning, you wont understand anything, please go to post number " + number
>>>
If you are not reading this post from the beginning, you wont understand anything, please go to post number 1
>>>

Well done, so here we printed out what we wanted, the function str(), says "Hey, Python, anything you see inside me, is a string"

Python now understands that the number 1, is actually a string, and concatenates it.

One more way, to make the Python believe that something is a string is to use back-ticks, ( ` ) symbol, i know its kind of hard to see the symbol, but its the key below Esc Key and the key above the Tab key, (~ key)

So here is how we use it:

>>> t1 = 9
>>> t2 = 26
>>> print "The time now is " + `t1` + ":" + `t2` + " PM"
The time now is 9:26 PM
>>>

There is one more way actually using the repr() function, but will discuss about it later, for now, practice with these, otherwise you will lost with the next posts.

Was it Interesting?? I know its kind of boring, it was boring for me too, but you have to know about these.

So practice with these, know how to use them, and don't forget to check my next post.

Thank You.

3 comments:

  1. Nice.......I luv the way u use.....
    like when u say string says this to python etc. etc. Its a nice way of learning for noobs....

    ReplyDelete