String
String
characters.
st=“ I am fine”
st=‘we are good’
print(st)
Multiline strings can be written 2 ways.
Multiline string can be enclosed in a single
quote with back slash at the end of every
line as shown below:
st=“i\
am\
good.”
Multiline string can be enclosed in triple
quote as shown in the following statement
st=“ “”I
Am
Good.”””
length of iamgood is 7
length of i
am
good is 9
Strings can be fetched both in the forward
direction and backward direction
-9 -8 -7 -6 -5 -4 -3 -2 -1
E X C E L L E N T
0 1 2 3 4 5 6 7 8
Single element of string can be printed in
the following manner by using either
forward index or backward index.
-9 -8 -7 -6 -5 -4 -3 -2 -1
E X C E L L E N T
0 1 2 3 4 5 6 7 8
st=“excellent”
Print(“e” in st)—True
Print(“x” in st)-False
Relational operators can be used with
strings
s1=“good”
s2=“Good”
print(s1==s2)
print(s1<s2)
print(s1>s2)
print(s1<=s2)
print(s1>=s2)
Print(s1!=s2)
Output:-
False
False
True
False
True
True
ord(‘a’) function is used to convert the
character to respective ASCII value
C=ord(‘a’)
Print©--output 97
chr(97)-is used to convert the given ASCII to
the respective character
C=chr(97)
print©-’a’
String slices:-
wor
zing wor
amazi
print(str[ :-5])
print(str[-12:-5:1])
print(str[9:-5:1])#no output
print(str[0:-5:1])#important
print(str[0:-5:-1])#no output
print(str[:-4:-1])
print(str[:-4:])
OUTPUT:
amazing
amazing
amazing
kro
amazing
str="excellent"
print(str[4:+4])
print(str[:4]+str[:5])
print("printg entire string")
print(str[:4]+str[4:])
print(str[2:8:2])
print(str[1:])
OUTPUT:
exceexcel
printg entire string
excellent
cle
xcellent
print("printing reverse of the string")
print(str[::-1])
print(str[-1: :-1])
print(str[::1])
print(str[-1:-1 :-1])#no output
OUTPUT:
l
le
OUTPUT:-
enter the string i love my country
I love my country
2.Find() function finds the index of sub
string from initial index to final index.
str=input("enter the string")
sub="love"
print(str.find(sub, 2,9))
Output:-
enter the string
i love my lovely country
2
3. Islower() checks whether string is in lower
or not. If lower returns True else returns
False
str=input("enter the string")
# If accepted string is in lower case
print("is lower")
print(str.islower())
OUTPUT:-
is lower
True
4. isupper() checks whether string is in upper
case or not. If upper returns True else returns
False
str=“bright”
print(str.isupper())
Output_
False
5. isspace()- checks whether the character is
space or not. If str contains only space then it
returns True otherwise it returns False
st=“bright”
print(str.isspace())
Output:
False
st=" “
print(st.isspace())
OUTPUT:-
True
6. isalpha() checks whether it contains
alphabet or not . If string contains only
alphabets then return True othewise returns
False
str=input("enter the string")
print(str.isalpha())
Output:-
enter the string this is good123
False
7. isdigit() checks whether it contains only
digits or not . If string contains only digits
then return True othewise returns False
str=input("enter the string")
print(str.isdigit())
OUTPUT:
True
9. lower()- converts the string to lower case
str=“Go Slow”
Output:
go slow
10. upper()- converts the string to upper
case.
print(str.upper())
GO SLOW
11.strip()- strip off the space on both sides
of the string if string contains space
st=“ excellent ”
print(st.strip())
OUTPUT-excellent
12.lstrip(para)- strip off the characters
passed as parameter from strings if they
occur in contiguous cells of the string from
left hand side of the string.
st=“excellent”
print(st.lstrip("ell"))
OUTPUT:
xcellent
13. rstrip(para)- strip off the characters
passed as parameter from string if they
occur in contiguous cells of the string from
the right hand side of the string.
st=“excellent”
print(st.rstrip(“ent"))
OUTPUT
excell
st="saregamapadhanisa"
print(st.rstrip("races"))
print(st.rstrip("tears"))
print(st.rstrip("asian"))
print(st.lstrip("sargam"))
Output:-
saregamapadhani
saregamapadhani
saregamapadh
egamapadhanisa
14. join() function joins the character after
every character of the string which is
passed as a parameter
st2=“*”
st=“excellent”
st1=st2.join(st)
print(st1)
OUTPUT:
e*x*c*e*l*l*e*n*t
Join() function taking list of strings as a
parameter
It will join both strings of the list by inserting
character which is used for joining as shown
in the following example.
st2=“*”
st=“excellent”
st1=st2.join([“very”,st])
print(st1)
OUTPUT:
very * excellent
15. split()-function splits the string at the
character which is passed as a parameter
resulting to list of strings excluding the passed
character.
st2=“*”
st=“excellent”
st1=st2.join([“very”,st])
print(st1)
s=st1.split(st2)
print(s)
OUTPUT:
very * excellent
s=“this is very good”
st1=s.split(“ ")
print(st1)
OUTPUT:
Accept
beauty
beauty beautiful
beauty
beauty beautiful
18. startswith() checks whether strings
starts with character passed as a
parameter, if so returns true otherwise
returns false.
st=input("enter the string")
print(st)
if st.startswith("e"):
print(st)
else:
print("does not start with e")
Output
st=“excellent”
print(st.index(“l”))
Output
4
23. Len() is used to find out the length of
the string and returns the length.
st=“ Excellent”
l=len(st)
print(l)
Output: