Strings and built-in methods

In previous Strings in Python and Strings in Python more features articles, you have learnt about string, string declarations in memory and so on. In this article you will learn about string and methods. Python provides a rich gallery of methods which makes programmer’s life easy to perform different tasks. In this article you will see few of them and also you will see how to access a complete list of methods.

Most of the methods are self explanatory, for example capitalize() method makes first letter capital, upper() and lower() turn string into upper and lower case respectively. See following example:   

mystring = "Hello software Engineers, i am Python"
print(mystring)

## Capitialize first character of string 
print(mystring.capitalize())

## Each word's first character capitalized
print(mystring.title())

## All upper case
print(mystring.upper())

## All lower case
print(mystring.lower())


sub = "software"

# Return starting index of substring , -1 otherwise
print(mystring.find(sub))

# Start searching from index 4, return starting index of substring , -1 otherwise
print("found", mystring.find(sub, 4))

# Search between index 4 and 9, return starting index of substring , -1 otherwise
print("found", mystring.find(sub, 4 , 9))

characters = "abcdefg"

# Is string alphabatic
print("Is this string alphabatic" , characters.isalpha())

# Is string upper case
print("Is all characters uppercase" , characters.isupper())

characters = " abcdefg "
# remove white space from both sides of the string
print(characters.strip())

# remove white space from right side of the string
print(characters.rstrip())

# remove white space from left side of the string
print(characters.lstrip())

## Count character occurance 
print(mystring.count("E"))

letter ='e'
print("Letter " +letter+" appears" , mystring.count(letter) , "times")

This is a very quick introduction of string related methods and now lets see how to access complete list. Open IDLE python shell and create a string variable. For example mystring = “hello”, once the string variable is created type dir(mystring), you will see a complete list of methods. 

Screen Shot 2016-05-23 at 20.07.43

This is a long list of magical methods which are provided by Python. Few of them are already implemented in previous example including upper lower, capitalize, isalpha etc but there are few methods which start with underscore sign __add__ or __class__. Don’t be afraid, these functions are very easy to use. Check following example.


__add__ method

Python provides “+” sign to combines two strings and __add__ function does exactly the same.

str1 = "hello"
str2 = "hi"

## combine two strings
print(str1 + str2)
print(str1.__add__(str2))


__eq__ method

“==” sign compare two strings, __eq__ method does the same. Call __eq__ method with source string and pass target string as a parameter. __eq__ method returns true if both strings are equal, false otherwise. 

## == compare 2 string return true if equal otherwise false 
print("str1 is equal to str2 = ",str1==str2)

##__eq__ compare two strings return true if equal otherwise false
print("str1 is equal to str2 = ",str1.__eq__(str2))


__gt__ method

“>” sign compares two strings if first string is greater than second, __gt__ method also helps to find if one string is greater than other.

str1 = "hello"
str2 = "HELLO"

## > if str1 is greater than str2, return true otherwise false 
print("str1 is > str2 = ",str1 > str2)

## __gt__ checks if str1 is greater than str2, return true otherwise false
print("str1 is greater than str2 = ",str1.__gt__(str2))


__ne__ method

“!=” not equal sign compares two strings, it returns true if strings are not equal. The same job can be performed by __ne__ method. If strings are not equal, it returns true otherwise false.

## str1 is not equal to str2
print("str1 is Not Equal str2 = ",str1 != str2)

## __ne__ checks if str1 not equal to str2 
print("str1 is Not Equal to str2 = ",str1.__ne__(str2))


__len__ method

__len__ method returns the size of the string.

## __len__ returns size of string 
print("str1 length is = ", str1.__len__())