Skip to main content

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.

Example:
>>>s1="Python Programming"
>>>s2='Python Programs'
>>>s3="""Python is a powerful
Programming language"""

This will create a string object s1, s2 and s3 using different forms.

Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.

Example
>>>s="Python"
>>>s[0]
‘P’
>>>s[2]
‘t’
>>>s[5]
‘n’
>>>s[-1]
‘n’
>>>s[-3]
‘h’
>>>s[-6]
‘P’

We can also slice a string. Segment of a string is called a slice.

>>>s[1:3]
‘yt’

If the first index is not mentioned, the slicing will start from 0.
>>>s[:3]
‘Pyt’

>>>s[2:6]
‘thon’

If the last index is not mentioned, the slicing will go till the end of the string.
>>>s[3:]
‘hon’

We can also specify the step size in slicing. The example below slice 0,2 and 4th character.
>>>s[0:5:2]
'Pto'


We can also use –ve index in slicing
>>>s[1:-1]
'ytho'
>>> s[-4:-1]
'tho'
>>> s[-6:-2:2]
'Pt'

Negative step size makes string to be printed in reverse
>>> s[5:0:-1]
'nohty'

>>>s[-1:-7:-1]
'nohtyP'


Print the full string
>>> s[:]    or    >>>s[::]
'Python'

Print the string in reverse
>>> s[::-1]
'nohtyP'

Strings are immutable that is you cannot change the content of a string object
>>>s[2]=’c’ will leads to an error.

Reading strings

Note that when you use input method to read a string, input the string in quotes(single, double or triple quotes)

>>>S=input(“Enter string:”)
Enter String:”python”

The raw_input() method however consider the default input as string so you can enter the string without quotes.
>>>S=raw_input(“Enter string:”)
Enter String:python

Accessing characters in String (Traversing)
For loop is the best choice for accessing each character from a string.
The following program will count number of ‘a’ in the given string.
S=raw_input(“enter the string:”)
for c in S:
            if c==’a’:
              count+=1
print “Number of a=”, count
We can also use a while loop. In this case the string length have to be computed for testing the condition
S=raw_input(“enter the string:”)
i=0
while i<len(S):
            if S[i]==’a’:
              count+=1
            i+=1
print “Number of a=”, count

String comparisons

Strings can be compared with relational operators(==,!=,<,<=,>,>=) like numbers however they are compared based on the ASCII values of the characters

The following program will check whether the given string is palindrome or not

s=raw_input("Enter a String:")
rs=s[::-1] # rs is the reversed string
print "reversed String",rs
if s==rs:
    print "Palindrome"
else:
    print "Not Palindrome"


String Operations

The following operators can be applied to string apart from slicing.

+ operation can be used as concatenation operation.

>>>s1=”Python”
>>>s2=”Programming”
>>>s3=s1+s2
>>>s3
‘Python Programming’

‘*’ operation repeats a string specified number of times

>>>s4=s1*3   or   >>>s4=3*s1
>>>s4
‘PythonPythonPython’

“in” and “not in” operator can be used to check whether a substring is present in another string

>>>”Py” in s1
True
>>>”Py” not in s1
False

String Methods
Here are some of the most common string methods. A method is like a function, but it runs "on" an object. If the variable ‘s’ is a string, then the code s.lower() runs the lower() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, OOP).
The built in functions like len(),min(),max() etc can be used directly on string object. You can import string module and use string methods or with a string object call these functions.
Here are some of the most common string methods:
Let s  is Python string then  s.lower() and s.upper() will return the lowercase and uppercase versions of the string s.islower() and s.isupper() can be used to test whether s is lower case or upper case.
Example

>>>s=”Python programming”
>>>s.upper()
PYTHON PROGRAMMING
>>>s.lower()
python programming

s.isalpha(),s.isdigit(),s.isalnum() s.isspace() will test for alphabets, digits, alpha numeric and space.
s.find() and s.index() can be used to find a substring in the given string.It returns the position(idex) of the first occurrence of the substring. If it is not present it will return  -1 in case of find() method and throws an error in case of index() method.We can also specify the start and end position of the string index so that the search will be done in this range only.We can also use rfind()  to search for a substring from the right end of the string.

>>>s.find(‘pr’)
7
>>>s.find(‘to’)
-1
>>> s.find('o')
4
>>> s.rfind('o')
9
>>>s.find(‘pr’,8,-1)
-1
>>> s.find('ton')
-1
>>> s.index('ton')
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    s.index('ton')
ValueError: substring not found
>>>s.index(‘on’)
4
s.count() can be used to count the number of occurrence of a substring.
>>>s.count(‘m’)
2
>>>s.count(‘mii’)
0
s.title() converts into a title my making first character of each word capital.s.istitle() can be used to test whether is string is title cased.
>>>s.title()
‘Python Programming’

s.capitalize()     Return a copy of the string S with only its first character  capitalized.The remaining characters are turned into small case.
>>>s.capitalize()
‘Python programming’
>>>"hello MAN".capitalize()
'Hello man'

s.swapcase() will swap the case of each character.(upper case into lower case and vice versa)
>>> s.swapcase()
'pYTHON pROGRAMING'

s.replace(old,new) will replace all the occurrence of old substring with new
>>> s.replace('m',"M")
'Python PrograMMing'

s.split(char) will take a character and split s based on char. This function can be used to split a string into words with space as split character.

>>> s.split(" ")
['Python', 'Programming']
>>> s.split("n")
['Pytho', ' Programmi', 'g']
The partition() function will partition the string into 3 parts.string before partition  string, the partition string and the string after partition string.
>>> s="this is a test"
>>> s.partition("is")
('th', 'is', ' is a test')

The lstrip() and rstrip() finctions can be used to strip white spaces or a substring from left or right end. strip() method is used to do the stripping from both the ends. When no substring is specified, white spaces are striped.
>>> s="this is a test"
>>> s.lstrip('th')
'is is a test'
>>> s.rstrip('st')
'this is a te'
>>> s.strip('t')
'his is a tes'

join() method joins the string elements with a separator.
>>> ":".join(s)
'P:y:t:h:o:n: :P:r:o:g:r:a:m:m:i:n:g'

This will create a reversed string
>>>’’.join(reversed(s))

We can import string package and use advanced functions. For example the translate() function can be used in the following way.
from string import maketrans   # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print str.translate(trantab)
This will produce following results where each character in the intab is translated into corresponding outtab characters.

th3s 3s str3ng 2x1mpl2....w4w!!!

To know more about string module functions refer  https://www.tutorialspoint.com/python/python_strings.htm


Now  try the following exercise in lab

Consider the string str="Python Programming"

Write statements in python to implement the following
a) To display the last four characters.
b) To display the substring starting from index 4 and ending at index 8.
c) To check whether string has alphanumeric characters or not.
d) To trim the last four characters from the string.
e) To trim the first four characters from the string.
f) To display the starting index of the substring  'gr'.
g) To change the case of the given string.
h) To check if the string is in title case.
i) To replace all the occurrences of letter 'm' in the string with '*'

 String constants

The following are some of the string constants in string module
Example
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

>>> string.digits
'0123456789'

>>>string.hexdigits
'0123456789abcdefABCDEF'

>>> string.octdigits
'01234567'

>>> string.punctuations
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}-'

>>> string.whitespace
'\t\n\x0b\x0c\r '

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!
"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}- \t\n\r\x0b\x0c'

sample program using string constant

This program will convert lowercase letters into uppercase and uppercase letters into lowercase.

import string
s=raw_input("Enter n")
ns=""
for i in s:
    if i in string.ascii_lowercase:
      ns=ns+i.upper()
    elif i in string.ascii_uppercase:
       ns=ns+i.lower()

print "New String=", ns

Note:
Regular strings in python are byte strings. For creating a Unicode string use the prefix ‘u’ on the string literal.
>>>ustring = u'A Unicode string’
A raw string which does not consider the special characters uses the prefix r.
>>>s=r’python \t python’
Now you can try the following program based on string.

1. Read a string of alphabets. Find the length. Number of vowels and consonants in the given sting.
2.Check whether the given string is palindrome or not. (use slicing..refer example...University question)
3. Check whether the given string is palindrome or not. (use  for loop to reverse the string Hint: take the character from last position to the beginning and concatenate to a new string)
4.Check whether the given string is palindrome or not with out reversing the string(use a loop and check character by character..refer example...university question)
5. Count the number of occurrences of a character in a given string.( do not use count())
6. Read a string and split the string at positions of the letter ‘s’ ( use split())
7. Print the positions of occurrences of a character(index) in a given string.
8. Program to remove all occurrence of a substring ( or a character) from a string.(refer example program below)
9. Program to replace all occurrence of a substring ( or a character) with a new string.
10. Change the case( small letter into capital and capital into small) of all  characters in a string. ( refer example above)
11. Print all the characters from a string till the first occurrence of a vowel. ( use break)
12. Print all the non vowel characters from a string.( use continue)
13.Read a string which consist of lower case/ uppercase letters and digits. Write a program to count each. Use string functions ( islower(), isupper(),isdigit())
14.Decimal to binary conversion without using built in function( refer example)
15.Convert the given decimal number to hexadecimal( use the logic of problem 14)
16.Remove all punctuation from a string.( use string.punctuation)
17.Remove all vowels from a string(university question)
18.Write a program to convert all small letters into capital letters.(university question)
19.Find the number of occurrence of a given sub-string(university question)

Sample programs
Remove all vowels from a string(university question)
vowels="AEIOUaeiou"
s=raw_input("enter the string")
ns=""
for char in s:
    if char not in vowels:
        ns=ns+char
print "new string after removing vowels=",ns   


palindrome checking without reversing the string ( university question)
s=raw_input("Enter the string")
if s==s[::-1]:
     print "palindrome"
else:
       print "not palindrome'
this can also be done using a loop by checking character by character
s=raw_input("Enter the string..")
beg=0
end=len(s)-1
while beg<end:
    if s[beg]!=s[end]:
            print "Not palindrome"
            break
beg+=1
end-=1
else:
       print "Palindrome"

Decimal to binary conversion
n=input("Enter the decimal number....")
bn=""
while n!=0:
    b=n%2
    bn=str(b)+bn
    n=n/2
print "The binary equivalent is....",bn

Remove all occurrence of a substring from a string
s=raw_input("enter the string..")
ss=raw_input("enter substring to remove..")
ls=len(s)         # length of the string
lss=len(ss)     # length of the substring
ns="" # new string
i=0
while i<ls:
    css=s[i:lss+i]         #css is the substring to be compared extracted from main string
    if css==ss:
        i=i+lss
    else:
        ns=ns+s[i]
        i=i+1
print "new string",ns


The above program can also be written with built in function replace()
str=raw_input("Enter string with..")
sstr=raw_input("Enter the substring to remove..")
nstr=str.replace(sstr,"")

print "string after removing substring..",nstr

Program to replace all occurrence of a substring with a new substring (university question)
s=raw_input("enter string..")
ss=raw_input("enter substring to remove..")
nss=raw_input("enter the substring to replace....")
ls=len(s)
lss=len(ss)
ns=""
i=0
while i<ls:
    css=s[i:lss+i]
    if css==ss:
        ns=ns+nss
        i=i+lss
    else:
        ns=ns+s[i]
        i=i+1
print "new string",ns


Summary
A string is a primitive datatype in most programming languages that represents an ordered sequence of characters. In most languages strings are delimited with double quotes ("this is a string"), but in Python strings can be delimited by double or single quotes ('this is also a string in Python').
To a computer, strings are conceptually different from numbers, so for example the string "222" has nothing to do with the integer 222. To convert between strings and numbers in Python use type conversion functions.
String Operations
Some Python operations have special behaviors on strings. Notably,
+          combines two strings in a process called concatenation. For example, 'Stan'+'ford' will evaluate to 'Stanford'.
*           repeats a string a (positive integral) number of times. For example, 'spam'*3 will evaluate to 'spamspamspam'.
x in s   will test if x is a substring of s. That is, x in s will return true iff some contiguous slice of s is equal to x.
s[i]       will return the i'th character of string s. Note that like other sequences string indices start at zero.
s[-i]     will return the i'th character from the end of string s, so s[-1] is the last character of s, s[-2] is the second-to-last character, etc.
s[0:5]    will return the first 4 characters. Strings can be sliced in this way. They're
            defined by a starting point, and ending point, and an increment with the
            following syntax:
s[start:end:increment]
Useful Functions
Most of these functions will work on any type of sequence, not just strings, but they're repeated here because their conceptual behavior might seem slightly different.
len(s)              returns the number of characters in the string s.
s.lower()         returns a copy of s, but with all uppercase characters converted to lowercase. (useful if you don't want a test to be case-sensitive)
s.upper()         returns a copy of s with all lowercase characters converted to uppercase.
s.split([sep])   returns a list whose elements are the words in the string. The
parameter sep is optional, by default strings are split on all whitespace (spaces, tabs, newlines). For example, 'To be, or not to be.'.split() will evaluate to ['To', 'be,', 'or', 'not', 'to', 'be.']. Note that split doesn't remove punctuation.
Searching
s.index(x)                   returns the index of the first occurrence of the substring x in s. If x isn't a substring an error is thrown.
s.find(x)                      does the same thing as index, but returns -1 if x isn't a substring, rather than throwing an error.
s.rindex(x) and s.rfind(x) (short for right index and right find) return the last occurrence of the substring x in s.
s.count(x)                   returns the number of times the substring x appears in s. Note that it only counts non-overlapping occurrences, so '000000'.count('000') will return 2.
s.replace(x, y)             replaces every occurrence of the substring x in s with y. For example, 'batCatRat'.replace('at', 'oy') will return 'boyCoyRoy'.
Stripping
s.strip()           returns s, but with any trailing whitespace removed. This is sometimes useful when working with files.
s.lstrip()         returns s, but with any preceding whitespace removed.

Popular posts from this blog

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…

User Defined Functions in Python

So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s) and are called User Defined Functions. These functions can then be combined to form a module which can then be used in other programs by importing them.
In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition.
To define a function keyword def is used. After the keyword comes an identifier i.e.name of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function. A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while executing. In Python, statements in a block are written with indentation. Usually, a block begins when a li…

Dictionary In Python

Dictionaries are similar to other compound types except that they can use any immutable type as an index. We can refer to a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair. A dictionary is an extremely useful data storage construct for storing and retrieving all key value pairs, where each element is accessed (or indexed) by a unique key. However, dictionary keys are not in sequences.
            Dictionaries are mutable.             Dictionaries are unordered. Items in dictionaries are accessed via keys and not via their position. A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionaries are unordered key-value-pairs.  Dictionaries don't support the sequence operation of the sequence data types like strings, tuples and…