Course
String Methods
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
String Methods
Python's built-in str class defines different methods. They help in manipulating strings. Since string is an immutable object, these methods return a copy of the original string, performing the respective processing on it.
The string methods can be classified in following categories −
- Case conversion
- Alignment
- Split and join
- Boolean
- Find and replace
- Formatting
- Translate
Case conversion
This category of built-in methods of Python's str class deal with the conversion of alphabet characters in the string object. Following methods fall in this category
Let us discuss these methods with examples
Alignment
Following methods in the str class control the alignment of characters within the string object.
Here is the detailed explanation of each of the above methods
Split and join
Python has the following methods to perform split and join operations
Print Page
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.
Boolean
Following methods in str class return True or False.
Let us discuss these methods with examples
Find and replace
Following are the Find and Replace methods in Python
Translation
maketrans()
The
maketrans()
method returns a mapping table. It maps each character from astr to a character at same index in bstr. The mapping table returned by this method may be used by translate() method to replace the characters.Syntax
var.maketrans(astr, bstr, cstr)
Parameters
- astr − This can be either dictionary or string. If only one parameter is supplied, this parameter must be a dictionary. If two or more parameters are given, this parameter has to be a string specifying the characters to be replaced.
- bstr − This is the string having corresponding mapping character.
- cstr − Optional. The string parameter specifies the characters to remove in the string.
Return Value
This method returns a translate table to be used
translate()
function.Example
var = 'Explicit is better than implicit.'table = var.maketrans({'i':'I'})print ("original string:", var)print ("translation table:", table)
var1 = var.translate(table)print ("Translated string", var1)
var = "Explicit is better than implicit."table = var.maketrans("than", "then")print ("original string:", var)print ("translation table:", table)var2 = var.translate(table)print ("Translated string", var2)
var = 'Explicit is better than implicit.'table = var.maketrans("is","as", "s")print ("original string:", var)print ("translation table:", table)var3=var.translate(table)print ("Translated string", var3)
When you run this program, it will produce the following output
original string: Explicit is better than implicit.translation table: {105: 'I'}Translated string ExplIcIt Is better than ImplIcIt.original string: Explicit is better than implicit.translation table: {116: 116, 104: 104, 97: 101, 110: 110}Translated string Explicit is better then implicit.original string: Explicit is better than implicit.translation table: {105: 97, 115: None}Translated string Explacat a better than amplacat.
translate()
The translate() method returns a string where each character is replaced by its corresponding character in the translation table created by the maketrans() method.
Syntax
var.translate(table)
Parameters
- table − A translation table created by the maketrans() method.
Return Value
This method returns a translated copy of the string.
Example
var = 'Explicit is better than implicit.'table = var.maketrans({'i':'I'})print ("original string:", var)print ("translation table:", table)
var1 = var.translate(table)print ("Translated string", var1)
var = "Explicit is better than implicit."table = var.maketrans("than", "then")print ("original string:", var)print ("translation table:", table)var2 = var.translate(table)print ("Translated string", var2)
var = 'Explicit is better than implicit.'table = var.maketrans("is","as", "s")print ("original string:", var)print ("translation table:", table)var3=var.translate(table)print ("Translated string", var3)
When you run this program, it will produce the following output
original string: Explicit is better than implicit.translation table: {105: 'I'}Translated string ExplIcIt Is better than ImplIcIt.original string: Explicit is better than implicit.translation table: {116: 116, 104: 104, 97: 101, 110: 110}Translated string Explicit is better then implicit.original string: Explicit is better than implicit.translation table: {105: 97, 115: None}Translated string Explacat a better than amplacat.
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.