Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

More String Methods in Python Programming Language


Method
Description
 String capitalize()
Converts first Character to Capital Letter

Return type of this method is String and the method is invoked using a String Object and takes no argument or parameter.
str1="i am Indian"
str2=str1.capitalize()
str2 would be 'I am Indian' where str1 will remain the same ('i am Indian')

For non alphabetic letter at the first position of the string, there will be no change.
str1="# i am Indian"
str2=str1.capitalize()

str2 would be '# i am Indian' 
 String center()
Pads string with specified character

Return type of this method is String and the method is invoked using a string object and takes int value and character (optional) as arguments or parameters. This method returns the invoking string centered in width specified in first argument. Padding is done using the specified character passed as second argument, if any character is passed. Default filler character is a space.


str = "Mera Bharat Mahan"

print str.center(21, '#')
The output would be  : "##Mera Bharat Mahan##"

Again the statement print str.center(21) would produce the output "  Mera Bharat Mahan  "

 String casefold()
converts to casefolded strings

Return type of this method is string and the method is invoked using a string object. The casefold() method returns a string where all the characters are lower case.


This method is similar to the lower() method, but the casefold() method is stronger, more aggressive, meaning that it will convert more characters into lower case.
 String count()
returns occurrences of substring in string

Return type of this method is int, the method is invoked using a string object and takes argument (s) or parameters. Syntax of this method is string.count(substring, int start, int end): The second and third arguments are optional. 'string' is the invoking string from where the substring will be searched. 'substring' is the string which will be searched in 'string'. 'start' indicates the index from where search will be performed and 'end' is the end index of searching. If the second and third arguments are not passed, the searching will be performed on the entire string.
 boolean endswith()
Checks if String Ends with the Specified Suffix

Return type of this method is boolean, the method is invoked using a string object and takes argument (s) or parameters. Syntax of this method is boolean endswith(substring, int start, int end): The second and third arguments are optional. 'string' is the invoking string from where the substring will be searched. 'substring' is the string which will be searched in 'string'. 'start' indicates the index from where search will be performed and 'end' is the end index of searching. If the second and third arguments are not passed, the searching will be performed on the entire string. If the search is successful, the method returns true otherwise false.

str = "this is testing";

suffix = "testing";
print str.endswith(suffix)
print str.endswith(suffix,20)

suffix = "is";
print str.endswith(suffix, 2, 4)
print str.endswith(suffix, 2, 6)

Output

True
True
True

False
 String expandtabs()
Replaces Tab character With Spaces

The expandtabs() method returns a copy of string with all tab characters '\t' replaced with whitespace characters.

Return type of this method is string, the method is invoked using a string object and may or may not take parameter while invoking the method. If the method is invoked without parameter then each '\t' will be replaced by 8 whitespace characters otherwise the number of whitespace characters will be same as the integer value passed as argument.

str = 'abc\txyz'
result = str.expandtabs()
print(result)

Output
abc        xyz
 String encode()
returns encoded string of given string
 String find()
Returns the Lowest Index of Substring

Return type of this method is integer, the method is invoked using a string object and takes a string, start index and end index as parameters. Second and third arguments are optional. 

The method searches the argument string in the invoking string and if found, it returns the first occurrence index otherwise returns -1. If the 2nd and 3rd arguments are passed, it will search the  argument string within the specified indexes.


str1 = 'Mata o mata'
result = str1.find('mata')
print("Substring 'mata':", result)
result = str1.find('pita')
print("Substring 'pita ':", result)

Output

7
-1

 String format()
formats string into nicer output
 String index()
Returns Index of Substring

The index() method is similar to find() method for strings with the same return type and same argument list.


The only difference is that find() method returns -1 if the substring is not found, whereas index() throws an exception.
 String isalnum()
Checks Alphanumeric Character


Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains alphanumeric characters only it returns true otherwise false.

str1 = "So234MA"
print(str1.isalnum())


str1 =  "So 234MA"
print(str1.isalnum())

str1 =  "HiSoma"
print(str1.isalnum())

str1 = "3156"
print(str1.isalnum())

Output

True
False
True
True
 String isalpha()
Checks if All Characters are Alphabets

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains alphanumeric characters only it returns true otherwise false.

str1 = "So234MA"
print(str1.isalpha())


str1 =  "Soma Das"
print(str1.isalpha())

str1 =  "HiSoma"
print(str1.isalpha())

str1 = "3156"
print(str1.isalpha())

Output

False
False

True
False
 String isdecimal()
Checks Decimal Characters

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains decimal only it returns true otherwise false.

str1 = "So234MA"
print(str1.isdecimal())


str1 =  "Soma Das"
print(str1.isdecimal())

str1 =  "HiSoma"
print(str1.isdecimal())

str1 = "3156"
print(str1.isdecimal())

Output

False
False

False
True


 String isdigit()
Checks Digit Characters

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains digits only it returns true otherwise false.

str1 = "So234MA"
print(str1.isdigit())


str1 =  "Soma Das"
print(str1.isdigit())

str1 =  "HiSoma"
print(str1.isdigit())

str1 = "3156"
print(str1.isdigit())

Output

False
False

False

True
 String isidentifier()
Checks for Valid Identifier
 String islower()
Checks if all Alphabets in a String are Lowercase

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the alphabets in the invoking string are in lower case only it returns true otherwise false. The string may contain digits, whitespaces.

str1 = "So234MA"
print(str1.islower())


str1 =  "Soma Das"
print(str1.islower())

str1 =  "hi soma"
print(str1.islower())



Output

False
False

True



 String isnumeric()
Checks Numeric Characters

A numeric character may be Decimal, Digit or Numeric

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the alphabets in the invoking string are numeric characters, the method would return true otherwise false.
 String isprintable()
Checks Printable Character

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the characters in the invoking string are pritable characters, the method would return true otherwise false.

Characters that occupies printing space on the screen are known as printable characters like letters and symbols, digits, punctuation and whitespace

s = 'We can print space'
print(s)
print(s.isprintable())

s = '\nNew Line Character is printable'
print(s)
print(s.isprintable())

s = '' "

print('\nEmpty string also printable', s.isprintable())
 String isspace()
Checks Whitespace Characters

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the characters in the invoking string are space, the method would return true otherwise false.

s1 = '   \t'
print(s1.isspace())

s1 = ' abcd '
print(s1.isspace())


Output

True
False
 String istitle()
Checks for Titlecased String

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument.

The method returns true if an uppercase character is followed by a lowercase character or uncased character and if an uppercase character proceeds a character that character must be an uncased character.

The method returns false if no uppercase characters, if uppercase follows a lowercase character with no whitespace between the characters, if uppercase follows another uppercase with no whitespace.


Whitespace is a non case character.

str1 = "Master Code Online"
 str1.istitle()
True

str1 = "master code online"
 str1.istitle()

False
 String isupper()
returns if all characters are uppercase characters

Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument.

The method returns true if all the characters in the invoking string are in uppercase.

The method returns false if all the characters in the invoking string are in lowercase.



str1 = "Master Code Online"
 str1.isupper()
False

str1 = "SOMA DAS"
 str1.isupper()


True
 String join()
Returns a Concatenated String
 String ljust()
returns left-justified string of given width
 String rjust()
returns right-justified string of given width
 String lower()
returns lowercased string
 String upper()
returns uppercased string
 String swapcase()


This post first appeared on Tutorial Site On Computer Programming Languages F, please read the originial post: here

Share the post

More String Methods in Python Programming Language

×

Subscribe to Tutorial Site On Computer Programming Languages F

Get updates delivered right to your inbox!

Thank you for your subscription

×