Python Strings
Python Strings
fruit = “banana”
fruit[ : 3] #ban
fruit[ 3 :] #ana
fruit[:] ?
STRING COMPARISON
• Equality Comparison
if word ==“banana!”
• Other Comparisons
if word < “banana”:
print ”Your word,”+ word + “,comes before banana.”
elif word > “banana”:
print ”Your word,”+ word + “,comes after banana.”
STRING COMPARISON (to be
continued…..)
• > and < comparison operations are useful for putting words in
alphabetical order:
• Uppercase letters ,numerals and special symbol comes before
Lowercase letters in Python.
• Find Function :
Try out
string.find(“banana”,”na”) #2
string.find(“banana”,”na”,3) #4, (starts from index 3)
string.find(“bob”,”b”,1,2) #-1, (checks
between 1 to 2
excluding 2 index)
CHARACTER CLASSIFICATION
• Character Classification is a recognition of character (lowercase or
uppercase) or it’s a digit.
• String module provides several constants that are useful for these
purposes.
• string.lowercase contains all the letters that the system considers to be
lowercase.
• string.uppercase contains all the letters that the system considers to be
uppercase.
import string
print string.ascii_lowercase
print string.ascii_uppercase
print string.digits
Three ways to recognize lowercase
Method 1 :
def isLower(ch):
return string.ascii_lowercase.find(ch) != -1
Method 2 :
def isLower(ch):
return ch in string.ascii_lowercase
Method 3 :
def isLower(ch):
return ’a’ <= ch <= ’z’
String Operations
Assignment Questions:
Q1: Write a program in python to reverse a string.
Q2: Write a program in python to check whether a given string is
palindrome or not.
Q3:Write a Python function that accepts a string and calculate the
number of upper case letters and lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
Q4:Write a Python function to check whether a string is pangram or
not. Note : Pangrams are words or sentences containing every letter of
the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"
Q5: Write a python program to count the occurrence(frequency) of a
particular character in a string.
Q6:Write a Python program to get a string made of the first 2 and the
last 2 chars from a given a string. If the string length is less than 2,
return instead of the empty string. Sample String : 'wakawama'
Expected Result : 'wama'
Sample String : 'wa'
Expected Result : 'wawa'
Sample String : ' w'
Expected Result : Empty String
Q7:Write a Python program to add 'ing' at the end of a given string
(length should be at least 3). If the given string already ends with 'ing'
then add ‘EE' instead. If the string length of the given string is less than
3, leave it unchanged.
Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringEE‘
Q8: Write a Python program to find the first appearance of the substring
'not' and 'poor' from a given string, if 'bad' follows the 'poor', replace the
whole 'not'...'poor' substring with 'good'. Return the resulting string.
Sample String : 'The lyrics is not that poor!'
Expected Result : 'The lyrics is good!'
Q10: Write a Python program to get a string from a given string where
all occurrences of its first char have been changed to '$', except the first
char itself.
Sample String : 'restart'
Expected Result : 'resta$t'