Strings in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modifies it, and then returns the results.

In Python we can create the strings by using single quotes, double quotes and even using triple quotes. For example:
Using single 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 first srting'
                      'This is my first srting'
                      >>>

Open in new window

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'
                      >>> 

Open in new window

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'
                      >>> 

Open in new window

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.

Example:  
>>> 'It's a nice place'
                      SyntaxError: invalid syntax
                      >>> 

Open in new window

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"
                      >>> 

Open in new window

We can also use the backslash ‘\’ to run the same string with single quotes.
Example:
  
                      >>> 'It\'s a nice place'
                      "It's a nice place"
                      >>>

Open in new window

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
                      >>>

Open in new window

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 '

Open in new window

>>> ''' "Sam asked "Did you get my key ?" to Hari'''
                      ' "Sam asked "Did you get my key ?" to Hari'
                      >>>

Open in new window

So, how do we use a string inside a variable? Lets pass a string inside variables:
>>> x="Steve"
                      >>> y="jobs"
                      >>> x+y
                      'Stevejobs'

Open in new window

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'

Open in new window

The “+” sign is used for concatenation. Without the ‘+’ symbol we cannot concatenate the strings of the variable as shown in this examples.

Now if we just add a space on the x variable like:
  >>> x="Steve  "
                       

Open in new window

And then run x+y the result will look as below:
>>> x="Steve  "
                      >>> x+y
                      'Steve  jobs'
                      >>>

Open in new window

We can also run the same variables like using a comma in between such as below:
>>> x,y
                      ('Steve', 'jobs')
                      >>>

Open in new window

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
                      >>> 

Open in new window

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?

The result we are looking for will be ‘EMPID = 1269’.

We get the result like this:
>>> 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

Open in new window

We can see that we are getting an error because the variable is a number and cannot be concatenated with strings that are characters.

So we use a n= str (1269) in place of “n= 1269”. This will only take the string of the number and then the result will be as below:
>>> n= str (1269)
                      >>> "EMPID =" +n
                      'EMPID =1269'
                      >>> print "EMPID =" +n
                      EMPID =1269
                      >>>

Open in new window

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
                      >>>

Open in new window


For more information on strings you can log on to http://python.org.
0
3,071 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (0)

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.