MyAI

Strings

You can define python variable in single quote, double quote or triple quote as long as ending with same.

Ex:

Index:

Ex: Name[0] -> S Name[1] -> u Name[-1] -> a Name[-2] -> l Name[-3] -> l

Name = "Sunil Marella"
print("Hello "+Name)
print("Value of String :",Name)
print("length of string : ",len(Name))
print("First char of string : ",Name[0])
print("Range of char first 3 : ",Name[:3])
print("Range of char after 3rd : ",Name[3:])
print("Range of char b/w 1 to 3 : ",Name[1:3])
print("Range of char consec 2nd char: ",Name[::2])
print("Range of char consec 2nd char: ",Name[2:7:2])
print("Reverse string :", Name[::-1])
print ("var[-9:-4]:", Name[-9:-4])

#string functions
print("Upper case : ",Name.upper())
print("Lower case : ",Name.lower())
print("Split case : ",Name.split())
print("split case l : ",Name.split('l'))

# .formate method
print('This is the string {} where curlbracket {} exist incl string value {}'.format('INSERTED','2nd INSERT',Name))
print('This is the string {0} where curlbracket {0} exist incl string value {1}'.format('INSERTED','2nd INSERT'))
print('This is the string {f} where curlbracket {b} exist incl string value {b}'.format(f='INSERTED',b='2nd INSERT'))
print('This is the string {Values} where curlbracket {b} exist incl string value {b}'.format(Values='INSERTED',b='2nd INSERT'))
Value = 100000/111
print("The result value {}".format(Value))
print("The result value {r:1.3f}".format(r=Value))
print("The result value {r:10.3f}".format(r=Value))

#python 3.6 onwards - fstring
Wife = "XYZ"
Wife_Age = 29
print(f'My wife name {Wife} and {Wife_Age} years old')

#Immutability you can not change single char
Surname = "Sarella"
print(Surname[1:])
Surname = 'M'+Surname[1:]
print("New surname: ",Surname)
print(2+3)
print('2'+'3')

Result:

Hello Sunil Marella
Value of String : Sunil Marella
length of string :  13
First char of string :  S
Range of char first 3 :  Sun
Range of char after 3rd :  il Marella
Range of char b/w 1 to 3 :  un
Range of char consec 2nd char:  SnlMrla
Range of char consec 2nd char:  nlM
Reverse string : alleraM linuS
var[-9:-4]: l Mar
Upper case :  SUNIL MARELLA
Lower case :  sunil marella
Split case :  ['Sunil', 'Marella']
split case l :  ['Suni', ' Mare', '', 'a']
This is the string INSERTED where curlbracket 2nd INSERT exist incl string value Sunil Marella
This is the string INSERTED where curlbracket INSERTED exist incl string value 2nd INSERT
This is the string INSERTED where curlbracket 2nd INSERT exist incl string value 2nd INSERT
This is the string INSERTED where curlbracket 2nd INSERT exist incl string value 2nd INSERT
The result value 900.9009009009009
The result value 900.901
The result value    900.901
My wife name XYZ and 29 years old
arella
New surname:  Marella
5
23

Main Page   Next Chapter: Python - Lists