String:- Collection of character which are enclosed within quotes is called String.
e.g:- “Ram”, “Mohan”
OR
‘Ram’, ’Mohan’, ’123’, ‘123@com_354’
In Python we can represent our string in two way.
1. Single Line String:- “Ram is a good Boy”
2. Multiline String:-
‘’’
Ram is a good boy.
He is study in class 10th .
He is good in study.
‘’’
⇒ S1=”Ram\t is a \n good boy”
#Output Ram is a
good boy
⇒ Using String inside String.
S2=”Ram is a ‘good’ boy” #Output Ram is a ‘good’ boy
S3 = ‘Ram is a “good” boy’ #Output Ram is a “good” boy
NOTE:- In Python Data type of String is str
e.g:- S = “Ram”
print(type(s)) #Output <class ‘str’>
1. Write a program to input name, address and phone number and print all details.
#Output
Indexing in String:- When we store String, internally it store in the form of Array(Collection
of element) where each character of String occupied one element. Which index starting
from 0 to total number of character -1.
In Python reverse index is also applicable that is starting from -1 to -number of character.
e.g:-
s1= “computer”
s1=
0 1 2 3 4 5 6 7
C O MP U T E R
-8 -7 -6 -5 -4 -3 -2 -1
e.g:- S1 = “Computer”
print(s1) #Output Computer
print(s1[0]) #Output C
print(s1[-1]) #Output r
l = len(s1)
print(l) #Output 8
Output:
FOR LOOP
OUTPUT
1. Write a program to input any string and print ascii value of print.
OUTPUT:
Slicing of String:- A Slice represent a part of string.
How to Slice:-
Stringname[start:stop:stepsize]
e.g:
--
#Output
NOTES:- In Python String are immutable, means the original content of string Object never
be modified.
e.g:-
s1 = “Computer”
s1[0] = “T” #Output:- Type Error : ‘str’ Object does not support item assignment
s2 = [Link]()
print(s2, s1) #Output:- COMPUTER Computer
String Handling Method:-
1. Upper():- This method convert string into Uppercase.
2. Lower():- This method Convert string into lowercase.
e.g:-
s1 = “Ram”
s2 = “MOHAN”
print([Link]()) #Output RAM
print([Link]()) #Output mohan
3. Strip():- It remove unnecessary space from string from both side.
1. Lstrip():- It remove Left space.
2. Rstrip():- It remove Right space.
S1 = “ Ram “
S2 = “ mohan “
Print([Link]()) #Output Ram
Print(len(s2)) #Output 7
Print(len([Link]())) #Output 6
Print(len([Link]())) #Output 6
4. Title():- This function convert first letter of each word of string in uppercase.
e.g:-
s1 = “Ram is a good boy”
s2 = [Link]()
print(s2) #Output Ram Is A Good Boy
5. Capitalize():- It convert only first letter of a string into uppercase.
e.g:-
s1 = “ram kumar”
print([Link]()) #Output Ram kumar
6. Replace(old, new):- It replace old substring with new substring.
e.g:-
s = “ram kumar”
print([Link](“kumar”,”Singh”) #Output ram singh
7. Find(substring):- It is used to search a substring into object. It return Index value .
e.g:-
s1 = “Ram is a good boy”
i = [Link](“good”)
Print(i) #Output 9
8. Index(substring):- It similar to the find(). It also return index value of substring but it
generate error, when substring not find.
e.g:-
s1 = “Ram is a good boy”
i = [Link](“bad”)
print(i) #Output -1
i = [Link](“bad”)
print(i) #Output Value Error: substring not found.
9. Count(substring):- It return total number of occurrence of given substring.
e.g:-
s1 = “Ram is a good boy. He is good in study”
c1 = [Link](“good”)
print(c) #Output 9
10. Startswith(sub)/endswith(sub) :- It check whether a string starts or ends with a
given substring.
e.g:-
s = “Ram is a good boy. He is good in study”
b = [Link](“Ram”)
print(b) #Output True
b = [Link](“ram”) #Output False
b = [Link](“study”)
print(b) #Output True
11. Split(separator):- This method convert string into list with given separator.
e.g:-
s = “Ram is a good boy, He is good in study”
lst = [Link](“,”)
print(lst) #Output [‘Ram is a good boy’, ‘He is good in study’]
lst = [Link](“ “)
print(lst) #Output [‘Ram’, ‘is’, ‘a’, ‘good’, ‘boy’, ‘He’, ‘is’, ‘good’, ‘in’, ‘study’]
12. Join(list):- It join the element of list in a single string.
e.g:-
lst = [“Ram”,”Mohan”,”Sohan”]
s = “-“.join(lst)
print(s) #Output Ram-Mohan-Sohan
13. Isdigit():- Only Digit
14. Isalpha():- Only letters
15. Isalnum():- Only letters or digit.
e.g:-
s1 = “Ram”
s2 = “12334”
s3 = “123Ram”
print([Link]()) #Output True
print([Link]()) #Output True
print([Link]()) #Output True
Q. Write a Program to input any string and replace all ‘a’ with ‘p’ character.
#Output
Formating the String:- Formating means presenting the string in clearly Understandable
manner. The format method is used to format string.
e.g:-
#Output
Q. How to print two digit after decimal.
#Output
Q. Set Left alignment
#Output
⇒ Centralize
#Output
Handling Multiple String:-
#OUTPUT
Q. Write a program to input five string and print it.
#Output