Python String
Python String
by
Dr. M. Vanitha
Assistant Professor,
Department of Computer Applications,
Alagappa University
1 Python by Dr.M.Vanitha
String
String is a sequence of characters which is enclosed
between either single (' ') or double quotes (" "), python
treats both single and double quotes same.
2 Python by Dr.M.Vanitha
String
Creating String
Creation of string in python is very easy.
e.g.
a=‘Computer Science'
b=“Informatics Practices“
3 Python by Dr.M.Vanitha
String
Output
4 Python by Dr.M.Vanitha
String
Iterating/Traversing through string
Each character of the string can be accessed
sequentially using for loop.
e.g.
str='Computer Science‘
for i in str:
print(i)
5 Python by Dr.M.Vanitha
String
Output
c
o
m
p
u
t
e
r
S
c
i
e
n
c
e
6 Python by Dr.M.Vanitha
String comparison
We can use ( > , < , <= , <= , == , !=) to compare two
strings. Python
Compares string lexicographically i.e using ASCII
value of the characters.
Suppose you have str1 as "Maria" and str2 as "Manoj" . The
first two characters from str1 and str2 ( M and M ) are
compared. As they are equal, the second two characters are
compared. Because they are also equal, the third two
characters ( r and n ) are compared. And because 'r' has
greater ASCII value than ‘n' , str1 is greater than str2 .
7 Python by Dr.M.Vanitha
String comparison
e.g.
print("Maria" == "Manoj")
print("Maria" != "Manoj")
print("Maria" > "Manoj")
print("Maria" >= "Manoj")
print("Maria" < "Manoj")
print("Maria" <= "Manoj")
print("Maria" > "")
8 Python by Dr.M.Vanitha
String comparison
Output
False
True
True
True
False
False
True
9 Python by Dr.M.Vanitha
Updating Strings
String value can be updated by reassigning another value in it.
e.g.
var1 = 'Comp Sc'
var1 = var1[:7] + ' With Python'
print ("Updated String :-
“,var1)
OUTPUT
10 Python by Dr.M.Vanitha
String Special Operators
e.g.
a=“comp”
B=“sc”
Operator Description Example
+ Concatenation – to add two strings a + b = comp sc
12 Python by Dr.M.Vanitha
Triple Quotes
It is used to create string with multiple lines.
e.g.
Str1 = “””his course will introduce the learner
to text mining and text manipulation
basics. The course begins with an
understanding of how text is handled by
python”””
13 Python by Dr.M.Vanitha
String functions and methods
a=“comp” b=“my comp”
Method Result Example
r=a.index(‘o
str.index() Returns index position of substring
m’) will be
1
String consists of only r=a.isalnum()
str.isalnum()
alphanumeric characters (no will return
symbols) True
String consists of only
str.isalpha()
alphabetic characters (no
symbols)
14 Python by Dr.M.Vanitha
String functions and methods
Method Result Example
str.isspace() String consists of only
whitespace characters
str.istitle() String is in title case
str.isupper() String’s alphabetic
characters are all upper
case
str.isspace() String consists of only
whitespace characters
str.istitle() String is in title case
15 Python by Dr.M.Vanitha
String functions and methods
Method Result Example
b=‘**comp’;
str.lstrip(char) Returns a copy of r=b.lstrin() will be
str.rstrip(char) the string with ‘comp’
leading/trailing
characters removed
Removes specific
str.strip(char) character from leading
and trailing position
b=‘my comp’;
r=b.split() will be
str.split() Returns list of strings as [‘my’,‘comp’]
splitted
16 Python by Dr.M.Vanitha
String functions and methods
17 Python by Dr.M.Vanitha
#Python Program to calculate the number of
digits and letters in a string
string=raw_input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)
18 Python by Dr.M.Vanitha
Searching for Substrings
METHOD NAME METHODS DESCRIPTION:
endswith(s1: str): bool Returns True if strings ends with
substring s1
startswith(s1: str): bool Returns True if strings starts with
substring s1
count(substring): int Returns number of occurrences of
substring the string
find(s1): int Returns lowest index from where s1
starts in the string, if string not
found returns -1
rfind(s1): int Returns highest index from where
s1 starts in the string, if string not
found returns -1
19 Python by Dr.M.Vanitha
Searching for Substrings
E.g. program
s = "welcome to python"
print(s.endswith("thon"))
print(s.startswith("good"))
print(s.find("come"))
print(s.find("become"))
print(s.rfind("o"))
print(s.count("o"))
20 Python by Dr.M.Vanitha
Searching for Substrings
Output
True
False
3
-1
15
3
21 Python by Dr.M.Vanitha
THANK YOU
22 Python by Dr.M.Vanitha