String

 

program to illustrate the strings:
a ="vishal"
b= 'vishal'
c='''vishal
and i'm good boy'''
d=''' pr said "the boy is smart " and then we catch up '''
print(a)
print(b)
print(c)
print(d)
print(type(a and b and c and d))

Output:










Program to illustrate the string operations(concatenation, slicing, indexing)
a = "hlo mr. dj"
b = "mera gana please!"
# concatenation of string
c = a+b
print (c)
# indexing
print (c[12])
print (c[-1]) # means the last character
# slicing
print (c[0:6])
print (c[:10]) # means 0 to 10 index
print (c[10:]) # means index starts from 11
print(c[::3]) # print after skipping 3 indexes
print (len(c)) # to print the length of string
# to find the end of the string
print (a.endswith("please!"))
print (c.endswith("please!"))
# counting of words and characters in string
print (c.count("dj"))
# by default capital first character
print (b.capitalize())
# to find the word
print (b.find("gana"))
# to replace the word with new one
print (c.replace("mr.", "Mr."))

# \n for new line
#\t for tab space
# \\ to print single backslash
Output:


















Program to send letter using templet of strings

letter_templet ='''Dear <|Name|>
You are selected
Date: <|Date|>'''
name = input("enter your name ")
date = input("\n enter date ")
letter_templet=letter_templet.replace("<|Name|>", name)
letter_templet=letter_templet.replace("<|Date|>", date)
print (letter_templet)

Output:
















No comments:

Post a Comment