INT102 2.2 String
INT102 2.2 String
Unit 2: Strings
Created By
Mr. Prince Verma
School of Computer Science & Engg.
©LPU CSE101 C Python
Programming
Lovely Professional University
@LPU INT102
String
• It is qualitatively different from Integer and Float type.
• Characters which collectively form a String is a Compound Data Type.
Eg.
fruit = “apple”
print (fruit[1])
Output: p
©LPU CSE101
@LPU C Python
INT102 Programming
String: Character Retrieval
• performed from left to right (0,1, & so on) or right to left(-1,-2, & so on).
E.g. Output:
a = "Lovely Professional University" a[0]: L
print("a[0]:",a[0]) a[5]: y
print("a[5]:",a[5]) a[15]: o
print("a[15]:",a[15]) a[29]: y
print("a[29]:",a[29]) a[-1]: y
print("a[-1]:",a[-1]) a[-10]: U
print("a[-10]:",a[-10]) a[-30]: L
print("a[-30]:",a[-30])
©LPU CSE101
@LPU C Python
INT102 Programming
Length of String
• Len(): Its is the inbuilt function to find the length of a string.
E.g.
a = “LPU”
b= “Lovely Professional University”
print(len(a))
print(len(b))
Output: 3
30
©LPU CSE101
@LPU C Python
INT102 Programming
Length of String
• To get the last letter we might try
length = len(fruit)
last = fruit[length] #ERROR
(as position of elements is from 0 to 6)
• Right Method to do this is :
length = len(fruit)
last = fruit[length-1]
• Another way to get the elements from last is :
fruit[-1] # yields the last letter
fruit[-2] # yields the second last letter
©LPU CSE101
@LPU C Python
INT102 Programming
Traversal of String using WHILE loop
• Processing one character at one time.
Eg.
a = “LPU”
index = 0
while index < len(a):
print (a[index]) Output: L
index = index + 1 P
U
©LPU CSE101
@LPU C Python
INT102 Programming
Traversal of String using FOR loop
• For loop provides us a privilege to access the characters without using index.
Eg.
a = "LPU"
for index in range(0,len(a)):
print (a[index])
Output: L
P
U
(Each time through the loop a character is assigned to the variable char)
©LPU CSE101
@LPU C Python
INT102 Programming
Traversal to count characters in String
• For and while loops can be used for looping and counting.
Eg.
fruit=“apple”
count = 0
for char in fruit:
if char == ’p’:
count = count + 1
print (“Total p’s in string:”,count)
©LPU CSE101
@LPU C Python
INT102 Programming
String Slicing
• A segment of a string is called a slice, i.e. a character.
Syntax: a[n:m],
where a is the strings, n is the starting index and m is the end index of slice.
Includes the first index and excluding the last index.
E.g.
s= “Peter, Paul, and Mary”
print(s[0:5])
Output: Peter
print(s[7:11])
Paul
print(s[17:21])
Mary
©LPU CSE101
@LPU C Python
INT102 Programming
String Slicing
E.g.
a = “Lovely Professional University"
print(a[ :6])
print(a[3: ]) Output: Lovely
print(a[7:17]) ely Professional University
print(a[ : ]) Profession
Lovely Professional University
print(a[-10:-1])
Universit
print(a[-10:]) University
print(a[:-17]) Lovely Profes
©LPU CSE101
@LPU C Python
INT102 Programming
Comparison of String
• Various comparison operators are:
==, <, >
• Equality Comparison: (==)
E.g.
word = “LPU"
if word == “LPU":
print("The words match.")
else:
print("The words doesn't match.")
Output: The words match.
©LPU CSE101
@LPU C Python
INT102 Programming
Comparison of String
• Comparison operators: (> and <) helps in putting words in alphabetical order:
– Uppercase letters ,numerals and special symbol comes before Lowercase letters
in Python.
Concatenation of String.
word = “LPU"
if word < “lpu":
print("Your word,"+ word + ",comes before lpu.")
elif word > “lpu":
print("Your word,"+ word + ",comes after lpu.")
Output: The word, LPU, comes before lpu.
©LPU CSE101
@LPU C Python
INT102 Programming
Strings are Immutable
• An existing string cannot be modified.
E.g. E.g.
greeting = “hello, world!” greeting = “hello, world!“
greeting[0] = ’H’ newGreeting = ’H’ + greeting[1:]
print(greeting) print(newGreeting)
©LPU CSE101
@LPU C Python
INT102 Programming
String Module
• It is a package which contains useful functions for manipulating strings
• To use the string module we need to import it first by using the following line
of code i.e.
import string
• Common String Module functions are:
– ascii_lowercase: returns all the letters that the system considers to be lowercase
– ascii_uppercase: returns all the letters that the system considers to be uppercase
– digits: returns all the digits that the system considers
©LPU CSE101
@LPU C Python
INT102 Programming
Three ways to recognize lowercase/uppercase
• E.g. In this case, assume:
import string
ch=‘a’
• Method 1 :
print(string.ascii_lowercase.find(ch) ==0)
• Method 2 :
print(ch in string.ascii_lowercase)
• Method 3 :
Output: True
print(’a’ <= ch <= ’z’)
©LPU CSE101
@LPU C Python
INT102 Programming
String Functions
• Other Functions on string are: (doesn’t require import string)
– lower(): returns the string in lowercase
– upper(): returns the string in uppercase
– capitalize(): returns the string with first character capital
– title(): returns the string with all words as first character capital
– islower(): returns True if the string in lowercase
– isupper(): returns True if the string in uppercase
– istitle(): returns True if all the words in string has first character capital
– isdigit(): returns the string with all words
– find(): returns the position of the char/substring in given string
– length(): returns the number of characters in strings
©LPU CSE101
@LPU C Python
INT102 Programming
String Functions
E.g. import string
a = "Lovely Professional University"
print("The lowercase characters are:",string.ascii_lowercase)
print("The uppercase characters are:",string.ascii_uppercase)
print("The digits are:",string.digits)
Output:
The lowercase characters are: abcdefghijklmnopqrstuvwxyz
The uppercase characters are: ABCDEFGHIJKLMNOPQRSTUVWXYZ
The digits are: 0123456789
©LPU CSE101
@LPU C Python
INT102 Programming
String Functions
E.g. a = "Lovely Professional University"
print("The lowercase of string is:",a.lower())
print("The lowercase of string is:",a.upper())
print("The title of string is:",a.title())
print("The capitalization of string is:",a.capitalize())
Output:
The lowercase of string is: lovely professional university
The lowercase of string is: LOVELY PROFESSIONAL UNIVERSITY
The title of string is: Lovely Professional University
The capitalization of string is: Lovely professional university
©LPU CSE101
@LPU C Python
INT102 Programming
String Functions
E.g. a = "Lovely Professional University"
b = "457"
print("Is the string lowercase:",a.islower())
print("Is the string uppercase:",a.isupper())
print("Is the string title:",a.istitle())
print("Is the string is digits:",b.isdigit())
Output:
Is the string lowercase: False
Is the string uppercase: False
Is the string title: True
Is the string is digits: True
©LPU CSE101
@LPU C Python
INT102 Programming
String Functions
E.g. a = "Lovely Professional University"
print("Index of 'University' in string:",a.find('University’))
print("Index of 'o' in string:",a.find('o’))
print("Index of 'o' in string after index-5:",a.find('o',5))
print("Index of 'o' in string between 5 and 15:",a.find('o',5,15))
Output:
Index of 'University' in string: 20
Index of 'o' in string: 1
Index of 'o' in string after index-5: 9
Index of 'o' in string between 5 and 15: 9
©LPU CSE101
@LPU C Python
INT102 Programming
Any
Questions?
©LPU CSE101
@LPU C Python
INT102 Programming