Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 'This is my first srting'
'This is my first srting'
>>>
Using double quotes:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> "This is my double quote string"
'This is my double quote string'
>>>
Using triple quotes:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> '''This is my tripe quote string'''
'This is my tripe quote string'
>>>
It really doesn’t matter what type of string we use unless we are looking to derive from the result. For example we are using something like “It’s a nice place ”, then in this case, because we have a single quote in our original string we cannot use single quotes before and after the sting.
>>> 'It's a nice place'
SyntaxError: invalid syntax
>>>
So to use this type of string we need to use double quotes such as below:
>>> "It's a nice place"
"It's a nice place"
>>>
We can also use the backslash ‘\’ to run the same string with single quotes.
>>> 'It\'s a nice place'
"It's a nice place"
>>>
Similarly the example below will show how to use the backslash when we are using double quotes within a double quote string:
>>> " Sam asked "Did you get my key ?" to Hari "
SyntaxError: invalid syntax
>>>
To run this string we can use the backslash or triple quotes as shown below:
>>> " Sam asked \"Did you get my key ?\" to Hari "
' Sam asked "Did you get my key ?" to Hari '
>>> ''' "Sam asked "Did you get my key ?" to Hari'''
' "Sam asked "Did you get my key ?" to Hari'
>>>
So, how do we use a string inside a variable? Lets pass a string inside variables:
>>> x="Steve"
>>> y="jobs"
>>> x+y
'Stevejobs'
Just to make a little difference on the above example, let’s put a space in between the Steve and Jobs.
>>> x+' '+y
'Steve jobs'
The “+” sign is used for concatenation. Without the ‘+’ symbol we cannot concatenate the strings of the variable as shown in this examples.
>>> x="Steve "
And then run x+y the result will look as below:
>>> x="Steve "
>>> x+y
'Steve jobs'
>>>
We can also run the same variables like using a comma in between such as below:
>>> x,y
('Steve', 'jobs')
>>>
Python converts the strings in two different ways. We have seen that with single, double and triple quotes, it displays the strings, but if we use the same examples using the PRINT command then it displays the strings as below:
>>> print "It's a nice place"
It's a nice place
>>>
In this example we didn't get the single quotes at beginning and end of our strings. This is important to note because we can also use a number as a string. For example, say we declared a variable called
n = 1269 and want it to be added to a string called
“EMPID =” then what should be the expected result?
>>> n= 1269
>>> "EMPID =" +n
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
"EMPID =" +n
TypeError: cannot concatenate 'str' and 'int' objects
>>>
>>> print "EMPID =" +n
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
print "EMPID =" +n
TypeError: cannot concatenate 'str' and 'int' objects
We can see that we are getting an error because the variable is a number and cannot be concatenated with strings that are characters.
>>> n= str (1269)
>>> "EMPID =" +n
'EMPID =1269'
>>> print "EMPID =" +n
EMPID =1269
>>>
We can also display the string with the number, without using the
str function, by putting them between a single quote. For
example let us take another variable called num= 1269.
>>> num=1269
>>> print "EMPID =" + `num`
EMPID =1269
>>>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)