Built-in string operations in Python

In built string functions

Python has some very interesting built in methods that can be used to manipulate strings. In this section you will learn about some very interesting methods.

Capitalize

Python has defined a method capitalize() to capitalize the first letter of the string.

var1 = '''apple a day
keeps doctor away'''
print(var1)
print('capitalize the first letter of the string')
print(var1.capitalize())

Download the code
Run the code

What you need to note here is that capitalize() does not alter the value of the string. Instead, it will create and return a new value of string which is placed at a different memory location.

So, if you try the following code, it will not display any change.

var1 = '''apple a day
keeps doctor away'''
print(var1)
print('capitalize the first letter of the string')
var1.capitalize()
print(var1) 

var1.capitalize() does not change the value of var1. Hence when you give the print(var1) command you will not see any change.

Converting the string to upper case or lower case.

To convert the entire string to upper or lower case you can use upper() or lower() method respectively.

var1 = 'Apple'
print(var1)
print('var1 in upper case is : '+ var1.upper())
print('var1 in lower case is : '+ var1.lower())

Download the code
Run the code

Formatting String to the center, left and right

In this section you will learn about three methods : center(), rjust() and ljust(). It can take two or one parameter. It is important to give the width for space padding the string. The second parameter is just a single character. Try the following code to see the outcome.

var1 = 'apple'
print(var1)
print('Format the string to the center')
print(var1.center(16,'*'))
print('Format the string to the right')
print(var1.rjust(16,'*'))
print('Format the string to the left')
print(var1.ljust(16,'*'))

Download the code
Run the code

Find out the length of a string

You can find the length of a string using len() method as shown below:

var1 = 'apple'
print(var1)
print('length of the string var1 is : ' + str(len(var1)))

Download the code
Run the code

Count how many times a substring repeats itself in a string.

Python allows you to find how many times a substring repeats itself in a string. The count method takes three parameters: (1) the substring that you want to search(2) The index of the string from where you want to start searching(3) The index of the string till which you want to search the substring.

Keeping this in mind look at the piece of code given below. var1.count(‘p’,0,len(var1) indicates that you want to count in var1, how many times substring ‘p’ repeats itself. Starting from index 0 which is the beginning of the string to the end that’s why the ending index’s value is equal to the length of the string var1.

var1 = 'apple'
print(var1)
print('count how many times letter p repeats in apple')
print(var1.count('p',0,len(var1)))

Download the code
Run the code

<<< Python string special operatorsBuilt-in string operations in Python -II >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .