String Operations in Python


In python, there is a standard library, called string. In the string module, there are different string related constants, methods, classes are available.

To use these modules, we need to import the string module in our code.

import string

Some string constants and their corresponding values are as follows −

Sr.No.String Constant & Values into it
1string.ascii_lowercase
‘abcdefghijklmnopqrstuvwxyz’
2string.ascii_uppercase
‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
3string.ascii_letters
Concatenation of asci_lowwecase and ascii_uppercase ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
4string.digits
‘0123456789’
5string.hexdigits
‘0123456789abcdefABCDEF’
6string.octdigits
‘01234567’
7string.punctuation
‘!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~’
8string.printable
All printable ASCII characters. It is collection of asci_letters, punctuation, digits and whitespace.‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~ \t\n\r\x0b\x0c’
9string.whitespace
‘\t\n\r\x0b\x0c’

Example Code

 Live Demo

import string
print(string.hexdigits)
print(string.ascii_uppercase)
print(string.printable)

Output

0123456789abcdefABCDEF
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

String Formatting

The built-in string class in python supports different complex variable substitution and value formatting by the format() method.

To format the string, the basic syntax is −

‘{} {}’.format(a, b)

The value of a and b will be placed at those places, which are enclosed by ‘{}’. We can also provide the positional parameters within the braces. Or writing some variable name is also valid within the braces.

Using this formatting option, we can also set the padding into our text. To add padding into the text the syntax is like this −

‘{: (character) > (width)}’.format(‘string’)

The ‘string’ will be padded using a specific character, with width, value to the right using the > symbol. We can use < to pad to the left. ^ to set at the middle.

The format() method can also truncate the string using a given length. The syntax is like −

 

‘{:.length}’.format(‘string’)

The string will be truncated up to the given length.

Example Code

 Live Demo 

print('The Name is {} and Roll {}'.format('Jhon', 40))
print('The values are {2}, {1}, {3}, {0}'.format(50, 70, 30, 15)) #Using positional parameter
print('Value 1: {val1}, value 2: {val2}'.format(val2 = 20, val1=10)) #using variable name

#Padding the strings
print('{: >{width}}'.format('Hello', width=20))
print('{:_^{width}}'.format('Hello', width=20)) #Place at the center. and pad with '_' character

#Truncate the string using given length
print('{:.5}'.format('Python Programming')) #Take only first 5 characters

Output


The Name is Jhon and Roll 40
The values are 30, 70, 15, 50
Value 1: 10, value 2: 20
Hello
_______Hello________
Pytho

String Template

The template string is used to substitute the string in simpler method. The template supports substitution using $ character. When the $identifier is found, it will replace it with new value of the identifier

To use the template into strings, the basic syntax is −

myStr = string.Template(“$a will be replaced”)
myStr.substitute(a = ‘XYZ’)

The value of a in the string will be replaced by ‘XYZ’ in the string. We can use dictionary to perform this kind of operations.

Example Code

my_template = string.Template("The value of A is $X and Value of B is $Y")
my_str = my_template.substitute(X = 'Python', Y='Programming')
print(my_str)

my_dict = {'key1' : 144, 'key2' : 169}
my_template2 = string.Template("The first one $key1 and second one $key2")
my_str2 = my_template2.substitute(my_dict)
print(my_str2)

Output

The value of A is Python and Value of B is Programming
The first one 144 and second one 169

Updated on: 25-Jun-2020

566 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements