String Literal

String literals can be enclosed by either double or single quotes.

Characters in a string can be accessed using the standard [ ] syntax, and like Java and C++, Python uses zero-based indexing, so if str is ‘hello’ str[1] is ‘e’. If the index is out of bounds for the string, Python raises an error.

Lets do some operations on strings in python using buit-in methods like,

String Conversion

String Comparison

String Padding

String Manipulation

String Conversion

Usage

# String conversion in Python # @IdiotInside_ str = "Idiot Inside" print ( "captilize() " ,str . capitalize()) print ( "casefold() " ,str . casefold()) #python 3.3 print ( "swapcase() " ,str . swapcase()) print ( "title() " ,str . title()) print ( "lower() " ,str . lower()) print ( "upper() " ,str . upper())

Output

String Comparison

Usage

# String Comparison in Python # @IdiotInside_ str2 = "Hello world" str3 = "Hello2world" str4 = "World" str5 = "hello" str6 = "PYTHON" dec1 = "10" dec2 = "10 USD" print ( "Checking if a string contains alpha numeric string or not:" ) condition1 = str2 . isalnum(); condition2 = str3 . isalnum(); print ( "String: " ,str2) if condition1: print (str2, " is an alphanumeric string" ) else : print (str2, " is not an alphanumeric string or contains space" ) print ( "String: " ,str3) if condition2: print (str3, " is a alphanumeric string" ) else : print (str3, " is not an alphanumeric string or contains space" ) print ( "

Checking if a string contains alphabets only:" ) condition3 = str2 . isalpha(); condition4 = str3 . isalpha(); condition5 = str4 . isalpha(); print ( "String: " ,str2) if condition3: print (str2, " is an alphabetic characters" ) else : print (str2, " is not a valid alphabetic characters" ) print ( "String: " ,str3) if condition4: print (str3, " is an alphabetic characters" ) else : print (str3, " is not a valid alphabetic characters" ) print ( "String: " ,str4) if condition5: print (str4, " is an alphabetic characters" ) else : print (str4, " is not a valid alphabetic characters" ) print ( "

Checking if a string contains decimals only:" ) condition6 = dec1 . isdecimal(); condition7 = dec2 . isdecimal(); if condition6: print (dec1, " is decimal" ) else : print (dec1, " is not a valid decimal" ) if condition7: print (dec2, " is decimal" ) else : print (dec2, " is not a valid decimal" ) print ( "

Checking if a string contains digits only:" ) condition8 = dec1 . isdigit(); condition9 = dec2 . isdigit(); if condition8: print (dec1, " is valid digit(s)" ) else : print (dec1, " is invalid" ) if condition9: print (dec2, " is valid digit(s)" ) else : print (dec2, " is invalid" ) print ( "

Checking if a string contains numerics only:" ) condition10 = dec1 . isnumeric(); condition11 = dec2 . isnumeric(); if condition10: print (dec1, " is numeric" ) else : print (dec1, " is not a valid numeric" ) if condition11: print (dec2, " is numeric" ) else : print (dec2, " is not a valid numeric" ) print ( "

Checking if a string contains lower case characters:" ) condition12 = str4 . islower(); condition13 = str5 . islower(); if condition12: print (str4, " is valid lower case string" ) else : print (str4, " is not a valid lower case string" ) if condition13: print (str5, " is valid lower case string" ) else : print (str5, " is not a valid lower case string" ) print ( "

Checking if a string contains upper case characters:" ) condition14 = str4 . isupper(); condition15 = str6 . isupper(); if condition14: print (str4, " is valid upper case string" ) else : print (str4, " is not a valid upper case string" ) if condition15: print (str6, " is valid upper case string" ) else : print (str6, " is not a valid upper case string" )

Output

String Padding

Usage

# String Padding in Python # @IdiotInside_ str8 = "Test" print ( "zfill():" ) print ( "150: " , "150" . zfill( 5 )) print ( "-150: " , "-150" . zfill( 5 )) print ( "ljust(s) :" ,str8 . ljust( 10 , "x" )) print ( "rjust(s) :" ,str8 . rjust( 10 , "x" ))

Output

String Manipulation

Program

# String Manipulation in Python # @IdiotInside_ str1 = "idiot inside" str2 = "hello idiot inside" str3 = "ab c



de fg \r kl \r

" str4 = " idiot inside " str5 = "idiot idiot inside inside" print ( "String: " ,str1) print ( "count('i') " ,str1 . count( 'i' ) ) print ( "find(i) " ,str1 . find( "i" )) print ( "find(s) " ,str1 . find( "s" )) print ( "rfind(i) " ,str1 . rfind( "i" )) print ( "replace() " ,str1 . replace( "idiot" , "geek" )) print ( "split() " ,str2 . split( ' ' )) print ( "split('1') " ,str2 . split( ' ' , 1 )) print ( "splitlines()" ,str3 . splitlines()) print ( "splitlines(True) " ,str3 . splitlines(True)) print ( "lstrip()" ,str4 . lstrip()) print ( "lstrip('idiot')" ,str5 . lstrip( "idiot" )) print ( "rstrip()" ,str4 . rstrip()) print ( "rstrip('inside')" ,str5 . rstrip( "inside" )) print ( "index('idiot')" ,str5 . index( "idiot" )) print ( "index('idiot',5)" ,str5 . index( "idiot" , 5 )) #print ("index()",str5.index("idiot",15)) returns error print ( "rindex(i'')" ,str1 . rindex( "i" )) print ( "rindex('i',5,7)" ,str1 . rindex( "i" , 5 , 7 )) #print ("rindex('o',5)",str1.rindex("o",5)) returns error

Output