count ( 'pay' ) ) #Count 'pay' in the string : 1 #Count 'o' in the first 20 ( 0, 20 ) characters of the string print (long_string. count ( 'o' ) ) #Count 'o' in the string : 3 print (long_string. Long_string = 'Your payment was been posted tonight.' print (long_string. split ( 'm' ) # 'm' is passed as argument print (splitted_m ) : - #To count the times a character ( or sequence of characters ) appears in a #string - > count ( ) split ( ) # by default it splits on white spaces print (split_w ) : # The output is list of strings #To split on the character 'm' ( lowercase 'm' ) upper ( ) print (full_name_age_sex_upper ) : 'MIKE SMITH, 30, MALE' - #To split the string on a specific character - > split ( ) #To convert all characters to uppercase - > upper ( )įull_name_age_sex_upper = full_name_age_sex. lower ( ) print (full_name_age_sex_lower ) : mike smith, 30, male Multiple_greeting = greeting * 4 print (multiple_greeting ) : hellohellohellohelloįull_name_age_sex = 'Mike Smith, 30, Male' - #To convert all characters to lowercase - > lower ( )įull_name_age_sex_lower = full_name_age_sex. #Index 012345678Ĭity = "San Diego" #Access part of a string : slicingĬhars = city #Letters a ( index 1 ) to D ( index 4 ) included print (chars ) : an DĬhars1 = city #First 5 characters 'San D' - option 1 print (chars1 ) : an DĬhars1 = city #First 5 characters 'San D' - option 2 print (chars1 ) : an DĬhars2 = city #Last 2 characters 'go' - option 1 print (chars2 ) : goĬhars2 = city #Last 2 characters 'go' - option 2 print (chars2 ) : goĮmpty = "" - #Length of a string - > len ( ) print ( len (name ) ) : 4 print ( len (city ) ) #Note : the white space contributes to the string's length : 9 print ( len (empty ) ) : 0 - #Check if a ( some ) character ( s ) is ( are ) present in a string - > in print ( 'a' in city ) : True Option 2 : use index - 1 print (char_last ) : oĬhar_last = city #The character before the last one : index - 2 print (char_last ) : g Option 1 : use index 8 ( length - 1 ) print (char_last ) : oĬhar_last = city #The last character. #Module aka remainder of a division - > % print ( 10 % 2 ) : 0 print ( 5.5 % 2 ) : 0.5Ĭity = "San Diego" - #Access single charactersĬhar1 = city #Letter D which is the fifth character, thus index 4 print (char ) : DĬhar2 = city #Letter S which is the first character, thus index 0 print (char2 ) : SĬhar3 = city #White space which is the forth character, thus index 3 print (char3 ) :Ĭhar_last = city #The last character.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |