logo codesdope

String in Python

You have already been introduced with strings.
a = "Hey there"
Like list, string is also a collection. They are collection of characters.

a = "Hey there"
print a[1]

Like list, we can also use 'len' and 'slice' for string.

a = "Hello World"
print len(a)
print a[1:4]

But strings are immutable. This means that we that we can't change any element of a string. So, we can't do a[2]='b' as we did with lists. Let's see what happens when we try to write this.

a =  "Hello World!"
a[2] = 'b'
a[2] = 'b'
TypeError: 'str' object does not support item assignment

You got this error because you can't change any element of a string.

We can also use for loop on strings. for loop will be discussed in a later chapter.

Let's play with string

import string
a = "Hey there world"
print a.split()
['Hey', 'there', 'world']

import string - We are making use of Python's string directory. It contains many useful functions. See more at Python's official documentation.
split function splits a string when it encounters a space(" ") and put them into a list.

To split at a particular place.

We can also use the split function to split the string with something other than a 'space'. Let's see how:

import string
a = "Buggy bug buggy bug, where are you my buggy bug!"
print a.split('b')
['Buggy ', 'ug ', 'uggy ', 'ug, where are you my ', 'uggy ', 'ug!']

Here we wanted our string to split at 'b'. So, we passed 'b' to the 'split()' function and it's done.

String comparison

Comparison of strings in Python is very simple. We just use '==', '<,' ,'>' ,'>=' , '<=' operators. Let's see an example:

a = raw_input()
if a>"abc":
	print 'your word comes after abc'
elif a<"abc":
	print 'your word comes before abc'
	print 'your word is abc'
your word comes after abc
Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters.

See this example of taking a string input of numbers which are separated by a space and storing them in a list as ints.

a = raw_input()
b = a.split()
print b
c = map(int,b)
print c
1 2 3 4 5 6 7 8 9
['1', '2', '3', '4', '5', '6', '7', '8', '9']
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Here, the 'split' split the string and stored them into a list with every space. And the'map' function changed all the strings of the list to ints.

Some more useful functions


Capitalizes the first character of a string

import string
a = "hey there world!"
print string.capitalize(a)
Hey there world!


Turns every character of the string into lower case.

import string
a = "hEy tHEre WOrld!"
print string.lower(a)
hey there world!


Turns every character of the string into upper case.

import string
a = "hEy tHEre WOrld!"
print string.upper(a)


Joins the element of a list to a string. It will be clear by the example given below.

import string
a = ["hEy", "tHEre", "WOrld!"]
print string.join(a)
print string.join(a,";")
hEy tHEre WOrld!


Inverts the case of a string

import string
a = "hEy tHEre WOrld!"
print string.swapcase(a)
HeY TheRE woRLD!

I have shown you many useful things to do with string. But there are a lot more functions to explore. You can use them whenever you need them from official documentation.

If you don't practice, you don't deserve to win.
-Andre Agassi

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question

Welcome.please sign up.


Welcome.please login.