Professional Documents
Culture Documents
String
String
#Indexing
print(name[1])
'''
String : R a m a
Index : 0 1 2 3
'''
#Looping
for x in "MacBook":
print(x)
for x in Name:
print(x)
print(" ")
print(x)
slice_from_beginning=Line[:6]
slice_till_end=Line[7:]
negative_slice=Line[-5:-2] #consider position or index as length-position
#string methods
upper_case=Line.upper() #coverts all character to Upper case
lower_case=Line.lower() #converts all characters to lower case
replace_character=Name.replace("a","the") #replace a with b
trim_spaces=Line.strip() #remove trailing spaces
split_string=Address.split(",") #splits the string and returns list
string_capitalize="sita".capitalize() #converts first char to upper case
string_center=Name.center(20,".")
print(string_center, len(string_center))
string_endsWith=Name.endswith("a") #returns True/False
string_endsWith1=name.endswith("a",1,3) #start and end index
#Format string
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {} dollars for {} pieces of item {}."
print(myorder.format(price,quantity,itemno))
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))