![]() ![]() If you’re coding in the Python programming language, it can quickly become overwhelming with all of the data types available. NET Microservices Certification TrainingĪSP.NET MVC with WebAPI Certification TrainingĪWS Solutions Architect Certification TrainingĪzure Fundamentals Certification TrainingĪrtificial Intelligence Certification Courseĭata Science with Python Certification Courseĭocker and Kubernetes Certification Trainingįrontend Foundations Certification Training ASP.NET MVC with WebAPI Certification Training.#Integer to string - > str ( ) print ( str ( 1 ) ) : 1 print ( type ( str ( 1 ) ) ) : - #String to integer - > int ( ) print ( int ( '56' ) ) #Valid conversion : 56 print ( type ( int ( '56' ) ) ) #Valid conversion : print ( int ( '56h' ) ) #Not valid conversion : ValueError : invalid literal for int ( ) with base 10 : '56h' - #Float to string - > str ( ) print ( str ( 3.5 ) ) : 3.5 print ( type ( str ( 3.5 ) ) ) : - #String to float - > float ( ) print ( float ( '23.5' ) ) #Valid conversion : 23.5 print ( type ( float ( '23.5' ) ) ) #Valid conversion : print ( float ( 'F23.5' ) ) #Not valid conversion : ValueError : could not convert string to float : 'F23. 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 |