String Function

 # String is a sequence of characters


a= 34           # it is print as integer
b = "sush"      # it is print as string
print(a, b)     #
print(a, type(a), b , type(b)) # type cast

#################################### string
story="once upon a time there was a student he know everything but he can't express the solution "

#string function
print (len(story))  # len(story) , len use to count characters there is 9 character in thin two words
print(story.endswith("upto")) # it says true or false what is at end of defined story
print (story.count("e"))  # how many e are there in the sentence
print (story.capitalize())  # its make first letter to Capital letter

print (story.find("time"))  #'time' is start after 12 letter 13th character, it finds only first time if there is another time it cant find the second

print (story.replace("upon", "there"))  #this can replaCE upon to there , all reoccurring words , all upon to there.

#####################################
# string slicing

greeting = "Good Morning"
name = "sushantsahu"
#print (type(name))

#print (greeting + name)  #both string added in single line is new string

# Concatenating two strings
#b = (greeting + name)
#print(b)

#print (greeting [7]) # it print 7th character of string start from zero ie 'r'
#'''  we cant change the item of string by
#name[3]="p" ,
#ie change 3rd character as 'p'''  # triple quote is comment'''
 
print (name[0:3])
print(greeting[1:7])
print (name[:3])     # is same as name [0:3]
print (name[0:])     # is same as name [0:up to last character of string]
print (name[0:9:2])  # 1to 9 every 2nd character is skipped

###############################

# Escape sequence character

story = "he is good. \n he is very good. " # \n enter to new line while printing
print (story)  # \n- new line \t- tab, \' - quote, \\ - backslash

#####################

# write a py program to display a user enter name followed by GoodAfternoon using input fn.
# Eg Good Afternoon Sush. ie string addition

# Name = input() # ("Enter your name ")
#Name = input("enter your Name")
Name = input("enter your Name\n")

#print("good afternoon, " + Name) # if you want to see it in a new line use \n new line command

print("good afternoon,\n " + Name)

##############################################

# Letter templet for an office

letter = '''
        Dear <|Name|>,
Greetings from PVM School,
Happy to inform you that
        Your are selected! for the post of \n Assistanc teacher.

        Have a Great day.

        Date: <|DATE|>
 
'''
name = input("enter Your Name\n")
date = input ("enter Date \n")
letter = letter.replace("<|Name|>", name)
letter = letter.replace("<|DATE|>", date)

print (letter)

##########################################

# to find double space we use Find string function in a string

st = "this is a string with  double spaces sentence" # -1 is no space, if any number ie at thate place there is a DS

doublespaces = (st.find("  "))  # double space , more than one DS it dont find second  
print (doublespaces)

st= st.replace("  "," ") # if there is any DS replace with SS single space and print it

print(st)

st= st.replace("sentence","word ") #it is a replace function
print(st)

####################################

#scape sequence CHARACTERS
#letter= "dear Sush, This is my new program to verify escape sequence character. thank you"

letter= "dear Sush,\n This is my new program\n\t to \t \n \t verify \n \t\" escape sequence character\" \n \t thank you! \n)"


print(letter)



### to be continue...

Comments

Popular posts from this blog

Game_01_Snake_Water_Gun_or_Rock_Paper_Scissor

Conditional Expression