You are on page 1of 2

String:

#Assign a String value to variables


name='Rama'
Name="Siva"
#Multi-line string
Address='''H-no: 555,
phase 1,
Kphb'''
#can also use double quotes(""" .... """) for multi-line 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)

#length of the string


print(len(name))
print(len("Chidrupa"))

Line='We are going to a temple.'


if "going" in Line:
print("Plan is fixed")

if "going" not in Line:


print("plan is cancelled")

#Slicing, considers Index


sliced_string=Line[7:13] #print from 7th index to 12th index
print(sliced_string)

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))

You might also like