Beruflich Dokumente
Kultur Dokumente
STRINGS
COMPOUND DATA TYPE
• Strings are qualitatively different from Integer and Float type.
• Characters which collectively form a String is a Compound Data Type.
For Eg.
fruit = “apple”
letter = fruit[1]
print (letter)
Output : p // (index value starts from 0 as in C & C++)
LENGTH OF STRINGS
• The inbuilt function to find the length of a string is ‘len()’.
For Eg.
fruit = “banana”
len(fruit)
Output : 6
• To get the last letter we might try
length = len(fruit)
last = fruit[length] #ERROR
( because there is no character at 6th place)
LENGTH OF STRINGS (to be continued…..)
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
A=“Banana”
A.find(”na”) #2
A.find(”na”,3) #4, (starts from index 3)
A.find(”b”,1,3) #-1, (checks between 1 to
3 excluding 3 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.
From string import*
print ascii_lowercase
print ascii_uppercase
print 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:
• Write a program to validate mobile number given by user. Consider
following validations:
1. It should only contain numbers
2. It should be of 10 digits only
3. It should start from 6, 7, 8 or 9 only
• Write a program to take the input from user for a string and a
substring. Find the entered sub-string in the given string and convert
it into uppercase as given in the example.
E.g String entered: John goes to school
Substring: goes
Output: John GOES to school
• Write a program to validate Email id given by user. Consider Following
validations:
1. It should not start with ‘@’
2. It should not end with ‘.’
3. There should be atleast 2 characters between ‘@’ and ‘.’
• Write a program to convert all lowercase letters to uppercase
letters, uppercase letters to lowercase, numbers to “#”, spaces with
“_” and all special characters to “1”
E.g. Given strings: asD gh1 $jKL8 95jkl bhg &&p
Answer: ASd_GD#_1Jkl# ##JKL_BHG_11P
• You are given a string .
Your task is to find out if the string contains: digits,spaces, lowercase
and uppercase characters.
• In the first line, print True if has any digits. Otherwise, print False.
• In the second line, print True if has any spaces. Otherwise, print False.
In the third line, print True if has any lowercase characters.
Otherwise, print False.
In the fourth line, print True if has any uppercase characters.
Otherwise, print False.
• In the fifth line, print True if has any special characters. Otherwise,
print False.
• You are given a string. Your task is to print only four characters in one
line. Next four characters should be in next line and so on till the end
of the string.
String entered: ABCDEFGHIJKLIMN
Answer: ABCD
EFGH
IJKL
IMN
• 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.
• Take the input from the user for a string. Write a Python
Program to get a string made of the first 2 and last 2
characters from a given string. If the string length is less
than 2, print “Empty String”.
E.g. String entered by user : google
Answer: gole