#3 Python Tutorial for Beginners | String in Python | Formatted String

Publié le: 24 novembre 2019
sur la chaîne: Rohan Paris
9
1

1) Strings :
They are contiguous set of characters represented in quotation marks.
Single quotes or Double quotes can be used
e.g. name1 = "Rohan"
name2 = 'Mohan'
print(name1)
print(name2)
Combination of single and double quote is not allowed. i.e a String in python should start and end with same quotation marks.
e.g. ERROR- name3 = "Sohan'
Subsets of strings can be represented using slice operator '[:]'
A slice operator takes two parameters seperated by colon
** [a:b] Here 'a' is the beginning position and 'b' is the end position of required string.
NOTE- [a:b] includes all characters starting from index position 'a' upto 'b' (i.e. character at position 'b' is not included)
** By default a string starts from position 0
e.g. str = "Elephant School"
print(str)
Elephant School
print(str[0])
E
print(str[3:11])
phant Sc
print(str[4:])
hant School
print(str[-4])
h
print(str[-9:-2])
nt Scho
print(str*2)
Elephant SchoolElephant School
** To clone a string-
str1= 'School of animals'
str2=str1[:]
print(str2)

2) Formatted Strings :
Use of formatted strings makes the visualization work easy for the code reader.
fname = "Rohan"
lname = "Paris"
msg = fname+ '['+lname+'] is a computer science student' # CONVENTIONAL WAY
print(msg)
msg2 = f'{fname} [{lname}] is a computer science student' # Using FORMATTED String
print(msg2)


Sur cette page du site, vous pouvez voir la vidéo en ligne #3 Python Tutorial for Beginners | String in Python | Formatted String durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Rohan Paris 24 novembre 2019, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 9 fois et il a aimé 1 téléspectateurs. Bon visionnage!