Python String functions: Basics of String and String Manipulation

Python Class TutorialPython is an object-oriented high level scripting language. Python is an interpreted type language, which means that it directly runs and executes line-by-line. Python is referred to as a very easy-to-learn language and supports many data types such as integers, floats, doubles, and booleans. Python does not support character type value but the string type stores the single letter in itself instead. Python also supports large number of functions for string manipulation.

Want to learn more about Python? Take up a tutorial at Udemy.com

String Declaration: A string can be declared using both single and double quotes.

Syntax:

str1 = 'Hello World!'

str2 = "Python Program"

Strings in Python can be accessed using the index number starting from 0 as the first index.

Learn Python through a tutorial at Udemy.com

For Example:

str1[0] would print ‘H’

str1[1] would print ‘e’ and so on.

To print group of characters

str1[0:6] would print “Hello”

Learn more about Python by taking a tutorial at Udemy.com

String Manipulations using String Functions:

capitalize(): This function returns the copy of the string passed changing the first character of the string to uppercase.

Syntax:

 str.capitalize()

Example:

str = "lets test the function";

print "str.capitalize() : ", str.capitalize() // change the first character to uppercase

Output:

str.capitalize() :  Lets test the function

 

islower(): This method checks if the string is  in lowercase and returns true if all the characters are in lowercase.

Syntax:

 str.islower()

 

Example:

str = "lets test the function";

print str.islower();// Returns true since all characters are in lower case.

str = "lets Test the function";

print str.islower();

Output:

True

False

isupper(): This method checks if all the characters in the string are in uppercase. If any character is in lower case, it would return false otherwise true.

Syntax:

str.isupper()

Example:

str = "LETS TEST THE FUNCTION";

print str.isupper();//returns true since all characters are capital

str = "LETS TEST THE FUNCTIOn";

print str.isupper(); // Returns false as 'n' is small.

Output

True

False

 

lower(): This method returns a string after converting every character of the string into lower case.

Syntax :

 str.lower()

Example:

str = "LETS TEST THE FUNCTION";

print str.lower();//converts the string to  lowercase

Output

lets test the function

upper():This method returns string after converting every character of string into lowercase

Syntax:

str.upper()

Example:

str = "lets test the function";

print str.upper(); //Converts the string to uppercase

Output:

LETS TEST THE FUNCTION

 

swapcase(): This method swaps the case of every character i.e. every uppercase is converted to lowercase and vice versa.

 

Syntax:

 str.swapcase()

Example:

str = "LETS test THE function";

print str.swapcase(); //swaps the cases

Output

lets TEST the FUNCTION

 

len():This methods returns the count of the total number of characters present in the string. It takes a string as argument. This string is the string length you want to calculate.

Syntax:

 len(str)

Example:

str2="Hello"

print “The length of string is ”len(str2);//Returns length of the string

Output:

The length of string is 5

 

split(): This function splits the string given as argument on the basis of the separator provided. If nothing is provided as argument, then it splits based on whitespaces.

Syntax:

 str.split()

Example:

str = "Lets test the function";

print str.split() //splits by every space it encounters

print str.split('the')// splits by ever "the" it encounters

Output:

 

[‘Lets’, ‘test’, ‘the’, ‘function’]

[‘Lets test ‘, ‘ function’]

 

replace(): This method returns the copy of the string in which a certain word is replaced by the given word.

Syntax:

 str.replace(old,new,max)
  • old: The substring to replace
  • new: This is the substring which is to be replaced in place of the old substring.
  • max: This arguments defines how many substrings would be replaced.

 

Example:

str = "Lets test the function and the test should be good";

print str.replace('test','changed')//Changes the value of 'test' to 'changed'

Output

Lets change the function and the changed one should be good

 

count(): The count method returns the count of occurrence of the substring in the string.

Syntax:

str.count(sub,start,end)
  • sub: This is the string to search.
  • start. Starting index of the search.
  • end: End index of the search

Example:

str = "Lets test the function and the test should be good";

sub="t"

print "Number of  t are",str.count(sub, 1, 20) //Counts total number of  't' present

Output

Number of  t are 5

lstrip(): This method returns the string after removing all the characters from the beginning of the string.

 

Syntax:

 str.lstrip([chars])

 

Example:

str = "             Lets test the function and the test should be good";

str1 = "000000000Lets test the function and the test should be good000000";

print str.lstrip(' '); // Removes all the beginning spaces.

print str1.lstrip(‘0’);// Removes all the zeros from starting

Output

Lets test the function and the test should be good

Lets test the function and the test should be good000000

 

rstrip(): This method returns the string after removing all the characters from the end of the string.

 

Syntax:

 str.rstrip([chars])

 

Example:

str = "Lets test the function and the test should be good               ";

str1 = "000000000Lets test the function and the test should be good000000";

print str.rstrip(' '); // Removes all the end spaces.

print str1.rstrip(‘0’);// Removes all the zeros from the end

Output

Lets test the function and the test should be good

000000000Lets test the function and the test should be good

 

rfind(): This method returns the last index of the substring found or otherwise -1 if the substring is not present.

Syntax:

str.rfind(str, beg,end)

 

  • sub: This is the string to search.
  • start. Starting index of the search.
  • end: End index of the search

Example:

str = "  Lets test the function and the test should be good";

print str.rfind(str);//String matches with itself

print str.rfind(str, 0, 10); //First 10 character matches only hence -1

print str.rfind('test', 0, 20);// Found at index 7

print str.rfind('and', 0, 30); // Found at index 25

Output:

0

-1

7

25

 

isdigit(): The method checks whether the strings consist only of digits and returns  true

or false accordingly.

Syntax:

str.isdigit()

 

Example:

str = "  1231231Lets test the function";

print str.isdigit(); //Returns false since string contains alphanumeric characters

str="12390877"

print str.isdigit();// Returns true since all characters are digits

Output

False

True

 

join(): This method is used to join the string based on the separator given.

Syntax:

str.join(seq)

Example:

str="-"

seq=("This","string","will","be","joined")

print str.join(seq) // The string is joined by the dash.

Output

This-string-will-be-joined