DAY 3 - Strings
PythonStrings
Python doesn't support character type. so a single letter will be treated as a string of length - 1 and index starts from 0.
val ="He is a good guy."
val1 = 'She is a good gal.'
> val[-1] # '.' > val1[1] #'h'
> val1[1:] # 'he is a good gal.'
> val[1:4] # 'e i'
> number = '123456789'
> even_number = number [1: :2] # '2468' pick every alternate digit from index = 1
> odd_number = number [0: :2] # '13579' pick every alternate digit from index = 0
> Sibling = 'RAKESH_RUPESH_RITESH_PUJA'
> first_Sibling,second_Sibling,third_Sibling,fourth_Sibling = Sibling.split('_') # 'RAKESH' 'RUPESH' 'RITESH' 'PUJA'
> 'PUJA' in Sibling True
> first_Sibling * 2 # 'RAKESHRAKESH' # multiply sequence by integer only
> first_Sibling + second_Sibling # 'RAKESHRUPESH' # can only concatenate str (not "int") to str
replace()
> val.replace('He','She') #'She is a good guy.'
> Sibling.replace('R','P',2) # 'PAKESH_PUPESH_RITESH_PUJA' replaced only 2 occurrences of R
upper() lower() capitalize()
> val.upper() # 'HE IS A GOOD GUY.'
> Sibling.lower() # 'rakesh_rupesh_ritesh_puja'
> 'he is A Good Guy'.capitalize() # 'He is a good guy'
RR Day3 Python Strings DataScience HappyLearning WeLearnEveryday
Comments
Post a Comment