Python Programming String Methods

len method

len method is used to get the number of characters in a string.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print("Number of characters: %d" % len(input_string)); 
Output:
$ python test_string_methods.py 
Number of characters: 26

count method

#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print("Substring occurence count: %d" % input_string.count("o")); 
Output:
$ python test_string_methods.py 
Substring occurence count: 3
we can also specify begining index and ending index to check.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print("Substring occurence count: %d" % input_string.count("o", 0, 10)); 
Output:
$ python test_string_methods.py 
Substring occurence count: 2
if not found any occurrence, returns 0.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print("Substring occurence count: %d" % input_string.count("z", 0, 10));
Output:
$ python test_string_methods.py 
Substring occurence count: 0

replace method

replace method is used to replace all the occurrence of substring in the string with new substring.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings welcome to!";                                    
                                                                                
print("replace method in input_string?: %s" % input_string.replace("welcome to", "learning"));
Output:
$ python test_string_methods.py 
replace method in input_string?: learning python strings learning!

split method

split method is used to split the string into list of words based on separator.
We can also specify the limit for split counts.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings welcome to!";                         
                                                                                
print("split method in input_string?: %s" % input_string.split(" "));           
print("split method with limit 2 in input_string?: %s" % input_string.split(" ", 2));
Output:
$ python test_string_methods.py 
split method in input_string?: ['welcome', 'to', 'python', 'strings', 'welcome', 'to!']
split method with limit 2 in input_string?: ['welcome', 'to', 'python strings welcome to!']

capitalize method

capitalize() method is used to capitalize the first letter of the string.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.capitalize()); 
Output:
$ python test_string_methods.py 
Welcome to python strings!

startswith method

startswith method is used to check whether string is starting with particular string or not.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.startswith("welcome"));  
Output:
$ python test_string_methods.py 
True
we can also specify begining index and end index range to check.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.startswith("welcome", 10, len(input_string)));
Output:
$ python test_string_methods.py 
False

endswith method

endswith method is used to check whether string is ending with particular string or not.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.endswith("strings!"));  
Output:
$ python test_string_methods.py 
True
we can also specify begining index and end index range to check.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.endswith("strings!", 0, len(input_string)-10));  
Output:
$ python test_string_methods.py 
False

index method

index method is used to find the substring matching index from index 0. if not matching, raises ValueError exception.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.index("strings!")); 
Output:
$ python test_string_methods.py 
18
we can also specify begining and ending index to check
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.index("strings!", 0, 20));     
Output:
$ python test_string_methods.py 
Traceback (most recent call last):
  File "test_string_methods.py", line 5, in 
    print(input_string.index("strings!", 0, 20));
ValueError: substring not found

find method

find method is used to find the given substring matching index similar like index method but difference here returns -1 if not matching give substring in the string.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.find("strings!"));  
Output:
$ python test_string_methods.py 
18
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print(input_string.find("strings!", 0, 20)); 
Output:
$ python test_string_methods.py 
-1

isalnum method

Returns True if all characters in the string are either alphabetic or numeric, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                                                                                                                         
                                                                                
print "is input_string alphanumeric?:", input_string.isalnum();  
Output:
$ python test_string_methods.py 
is input_string alphanumeric?: False
#!/usr/bin/python                                                               
                                                                                
input_string = "thisnumber2344";                                                
                                                                                
print "is input_string alphanumeric?:", input_string.isalnum();  
Output:
$ python test_string_methods.py 
is input_string alphanumeric?: True
No space and symbols in the string.

isalpha method

isalpha method returns True if all the characters in the string are only alphabetic characters, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome";                                                       
                                                                                
print "is input_string alpha:", input_string.isalpha();  
Output:
$ python test_string_methods.py 
is input_string alpha: True
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print "is input_string alpha:", input_string.isalpha(); 
Output:
$ python test_string_methods.py 
is input_string alpha: False

isdigit method

isdigit methdod returns True if all the characters in the string are only digits, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "2344";                                                          
                                                                                
print "is input_string digits?: ", input_string.isdigit(); 
Output:
$ python test_string_methods.py 
is input_string digits?:  True
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print "is input_string digits?: ", input_string.isdigit();   
Output:
$ python test_string_methods.py 
is input_string digits?:  False

islower method

islower method returns True if all the characters in the string are only lower case letters, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to python strings!";                                    
                                                                                
print "is lower input_string?: ", input_string.islower();   
Output:
$ python test_string_methods.py 
is lower input_string?:  True
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to python strings!";                                    
                                                                                
print "is lower input_string?: ", input_string.islower(); 
Output:
$ python test_string_methods.py 
is lower input_string?:  False

isupper method

isupper method returns True if all the characters in the string are only upper case letters, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "WELCOME TO PYTHON STRINGS!";                                    
                                                                                
print "is upper case letters only in input_string?: ", input_string.isupper(); 
Output:
$ python test_string_methods.py 
is upper case letters only in input_string?:  True
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to python strings!";                                    
                                                                                
print "is upper case letters only in input_string?: ", input_string.isupper(); 
Output:
$ python test_string_methods.py 
iis upper case letters only in input_string?:  False

isnumeric method

isnumeric method returns true if all characters in the string are numeric, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = "22343";                                                         
                                                                                
print("is numeric only in input_string?: %s" % str(input_string.isnumeric()));
Output:
$ python3 test_string_methods.py 
is numeric only in input_string?: True
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to python strings!";                                    
                                                                                
print("is numeric only in input_string?: %s" % str(input_string.isnumeric()));
Output:
$ python3 test_string_methods.py 
is numeric only in input_string?: False
Need to use python3 interpreter for isnumeric method, otherwise below error thrown
]$ python test_string_methods.py 
Traceback (most recent call last):
  File "test_string_methods.py", line 5, in 
    print("is numeric only in input_string?: %s" % str(input_string.isnumeric()));
AttributeError: 'str' object has no attribute 'isnumeric'

isspace method

ispace method returns True if all characters in the string are spaces, otherwise False.
#!/usr/bin/python                                                               
                                                                                
input_string = " ";                                                             
                                                                                
print("is spaces only in input_string?: %s" % str(input_string.isspace())); 
Output:
$ python test_string_methods.py 
is spaces only in input_string?: True

lower method

Converts the string in lower case letters.
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to Python Strings! ";                                   
                                                                                
print("lower case input_string?: %s" % input_string.lower()); 
Output:
$ python test_string_methods.py 
lower case input_string?: welcome to python strings! 

upper method

Converts the string in upper case letters.
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to Python Strings! ";                                   
                                                                                
print("upper case input_string?: %s" % input_string.upper());  
Output:
$ python test_string_methods.py 
upper case input_string?: WELCOME TO PYTHON STRINGS! 

lstrip method

lstrip method is used to remove leading specified characters in the string or removes leading spaces if not specified any characters.
#!/usr/bin/python                                                               
                                                                                
input_string = "   welcome to Python Strings! ";                                
                                                                                
print("lstrip input_string?: %s" % input_string.lstrip());                      
print("lstrip input_string?: %s" % input_string.lstrip(" wel")); 
Output:
$ python test_string_methods.py 
lstrip input_string?: welcome to Python Strings! 
lstrip input_string?: come to Python Strings! 

rstrip method

rstrip method is used to remove trailing specified characters in the string or removes trailing spaces if not specified any charaters.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome to Python Strings!  ";                                  
                                                                                
print("rstrip input_string?: %s" % input_string.rstrip());                      
print("rstrip input_string?: %s" % input_string.rstrip(" gs!"));
Output:
$ python test_string_methods.py 
rstrip input_string?: welcome to Python Strings!
rstrip input_string?: welcome to Python Strin

strip method

lstrip method is the combination of both lstrip and rstrip. removes both leading and trailing specified characters in the string. removes spaces if not specified chars list.
#!/usr/bin/python                                                               
                                                                                
input_string = " welcome to python strings!  ";                                 
                                                                                
print("strip method in input_string?: %s" % input_string.strip());              
print("strip method with chars in input_string?: %s" % input_string.strip(" welngs!"));
Output:
]$ python test_string_methods.py 
strip method in input_string?: welcome to python strings!
strip method with chars in input_string?: come to python stri

maketrans and translate methods

maketrans method is used to return translation table which contains mapping information for each charaters and translate method is used to convert the string using translation table.
#!/usr/bin/python                                                               
from string import maketrans;                                                   
                                                                                
input_string = "Welcome to Python Strings!  ";                                  
transtab = maketrans("omt", "123");                                             
                                                                                
print("maketrans method for input_string?: %s" % input_string.translate(transtab));
Output:
$ python test_string_methods.py 
maketrans method for input_string?: Welc12e 31 Py3h1n S3rings!  

max method

max method is used return max alphabetic character in the string.
#!/usr/bin/python                                                               
                                                                                
input_string = "Welcome to Python Strings!";                                    
                                                                                
print("max char in input_string?: %s" % max(input_string)); 
Output:
$ python test_string_methods.py 
max char in input_string?: y

min method

min method is used return min alphabetic character in the string.
#!/usr/bin/python                                                               
                                                                                
input_string = "welcome";                                                       
                                                                                
print("min char in input_string?: %s" % min(input_string)); 
Output:
$ python test_string_methods.py 
min char in input_string?: c

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^