Sunteți pe pagina 1din 5

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.

 Python 3

a = "Hey there"
print a[1]

Output

e
Like list, we can also use 'len' and 'slice' on a string.

 Python 3

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

Output

11
ell
But unlike lists, 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.

 Python 3

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.
Splitting a Stirng

 Python 3

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 a string with something other than a 'space'.
Let's see how:

 Python 3

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.

 Python 3

a = raw_input()
b = a.split()
print bc = map(int,b)
print c

Output

1 23 4 5 67 8 9
['1', '2', '3', '4', '5', '6', '7', '8', '9']
[1, 2, 3, 4, 5, 6, 7, 8, 9]
We first split the string using the 'split' function and stored them into a list. And then
the'map' function changed all the strings of the list to integers.

Useful String Functions

Let's have a look at some useful functions which we can use on a list.

capitalize

Capitalizes the first character of a string

 Python 3

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

Hey there world!

lower

Turns every character of the string into lower case.

 Python 3

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

Output

hey there world!

upper

Turns every character of the string into upper case.

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

Output

HEY THERE WORLD!

join

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

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

hEy tHEre WOrld!


hEy;tHEre;WOrld!

swapcase

Inverts the case of a string

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

Output

HeY TheRE woRLD!

You have seen many useful things to do with string in this chapter. But there are a lot
more functions to explore. You can use them whenever you need them from the official
documentation.

S-ar putea să vă placă și