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

Pubblicato il: 24 novembre 2019
sul canale di: 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)


In questa pagina del sito puoi guardare il video online #3 Python Tutorial for Beginners | String in Python | Formatted String della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Rohan Paris 24 novembre 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 9 volte e gli è piaciuto 1 spettatori. Buona visione!