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’

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
Remove characters at odd index positions from a string ( university question)
s=raw_input("Enter the string..:")
i=0
while i<len(s):
    if i%2!=0:
        s=s.replace(s[i],' ',1)
    i=i+1
print "New string:",s
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"

Slice the string to two separate strings; one with all the characters in
the odd indices and one with all characters in even indices.(uq)
s=raw_input("enter the string:")
eps=s[0:len(s):2]
print eps
ops=s[1:len(s):2]
print ops
Replace all the spaces in the input string with * or if no spaces found,put $ at the start and end of the string.(university question)
s=raw_input("enter the string:")
s=s.replace(" ","*")
if "*" not in s:
   s="$"+s+"$"
   print s
else:
  print s
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 ..")
sstr=raw_input("Enter the substring to remove..")
nstr=str.replace(sstr,"")
print "string after removing substring..",nstr
Converting all lowercase letters into uppercase ( university question)
import string
s=raw_input('Enter the string...')
ns=""
for c in s:
 if c in string.ascii_lowercase:
    c=chr(ord(c)-32)
    ns=ns+c
print "new string=",ns
Swapping case ..small letters to capital letters and capital letters to small letters.
s=raw_input('Enter the string...')
ns=""
sletter='abcdefghijklmnopqrstuvwxyz'
cletter='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for c in s:
  if c in sletter:
     c=chr(ord(c)-32)
  elif c in cletter:
    c=chr(ord(c)+32)
    ns=ns+c
print "new string=",ns
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
Reversing the first and second half of a string separately ( university question)
s=raw_input("Enter the string..:")
l=len(s)
fs=s[0:l/2]
ss=s[l/2:]
fs=fs[::-1]
ss=ss[::-1]
s=fs+ss
print "New string after reversal:::",s
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 white space removed. This is sometimes useful when working with files.

s.lstrip() returns s, but with any preceding white space removed.
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)

Comments

Post a Comment

Popular posts from this blog

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. Th

Identifiers,Variables and Keywords

Identifiers An identifier is a name given to entities like variables,functions,class, functions etc. It helps to differentiate one entity from another. Rules for writing identifiers 1.Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.  Names like myClass, var_1 and print_this_to_screen, all are valid example. 2.An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name. 3.Keywords cannot be used as identifiers. Eg: global = 1 is invalid 4.We cannot use special symbols like !, @, #, $, % etc. in our identifier. a@=0 is invalid 5.An identifier can be of any length. Things to Remember Python is a case-sensitive language. This means, Variable and variable are not the same. Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count=10 would make more sense, and it would be easier to figure out what it represents when you look at your code after

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.