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]
Output
e

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

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

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'
Output
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()
Output
['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')
Output
['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'
else:
	print 'your word is abc'
Output
as
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
Output
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


capitalize


Capitalizes the first character of a string

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

lower


Turns every character of the string into lower case.

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

upper


Turns every character of the string into upper case.

import string
a = "hEy tHEre WOrld!"
print string.upper(a)
Output
HEY THERE WORLD!

join


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,";")
Output
hEy tHEre WOrld!
hEy;tHEre;WOrld!

swapcase


Inverts the case of a string

import string
a = "hEy tHEre WOrld!"
print string.swapcase(a)
Output
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
Close

Welcome.please sign up.

Close

Welcome.please login.