In this article, learn how to transform strings to lowercase for accurate and efficient matching in your code. Find out how to center-align text within a specified width. Also, let’s check if a string ends with a specific suffix. Unleash the encoding capabilities of Python strings with the encode() method.
Before moving head, let’s take a look at Python Type Conversion.
Table of Contents
String
String is a sequence of characters enclosed within single or double quotes.
E.g., ‘Hello World’ , “Hello Python”
Let’s get started with some functions –
casefold() method
casefold() returns string of all characters in lower case. It converts more characters in lower case as compared to lower() method because it puts more pressure on string because of its strong and aggressive behaviour.
Syntax – string.casefold()
Parameter values – No
Return value – a lowercase string
String = "Hello! How Are You Doing?"
txt = String.casefold()
print(txt)
#output:
hello! how are you doing?
casefold() as Aggresive method
The casefold() method converts more string in lowercase as compared to lower() method. It has strong value and aggressive behaviour on string.
For example, the Germán letter ß is ss in english alphabet. The casefold() method converts ß it into english alphabet but lower() method does not.
String = "Viel Spaß!"
txt = String.casefold()
print(txt)
#output:
viel spass!
center() method
center() returns the string in the center position. The padding is the optional which is filled by fillchar (optional). By default the fillchar is filled by space.
Syntax – string.center(width[, fillchar])
Parameter values –
width – required.
fillchar – optional.
Return Type – returns modified string.
Example: Execute the center() method with default fillchar.
String = 'Python in Python'
text = String.center(50)
print(text)
#output
Python in Python
Example: Execute the center() method with custom fillchar.
String = 'Python in Python'
text = String.center(50, "^")
print(text)
#output:
^^^^^^^^^^^^^^^^^Python in Python^^^^^^^^^^^^^^^^^
endswith() method
endswith() returns True, if the string finished with specified suffix else returns False.
Syntax – string.endswith(suffix[, start[, end]])
or
in simply it can be – string.endswith( value, start, end )
Parameter values –
value – Required to check if the string ends with particular suffix.
start(Optional) – Optional argument which takes an integer as argument and specify the starting position of the string or at which position to start the search.
end(Optional) – Optional argument which takes an integer as argument and specify the end position of the string or at which position to start the search.
Example: Check if the string ends with !.
String = 'Python in Python!'
text = String.endswith("!")
print(text)
#output:
True
Example: Check if the string ends with !.
String = 'Python in Python'
text = String.endswith("!")
print(text)
#output:
False
Example: Check if the string ends with phrase.
String = 'Python is a language'
text = String.endswith("language")
print(text)
#output:
True
Example: Check if the string ends with phrase.
String = 'Python is a language'
text = String.endswith("language", 12, 20)
print(text)
#output:
True
Example: Check if the phrase present within specified range.
String = 'Python is a language'
text = String.endswith("language", 13, 20)
print(text)
#output:
False
It’s possible to pass a tuple suffixes to the endswith() method. If the string ends with any item of the tuple, it returns True. If not, it returns False.
Example: Check if the Tuple ends with particular string.
String = 'Python is a language'
text = String.endswith(("a","language"))
print(text)
#output:
True
Example: Check if the Tuple ends with particular string.
String = 'Python is a language'
text = String.endswith(("a","lang"))
print(text)
#output:
False
encoded() method
encoded() returns the string as converted encoded type. If no encoded is specified, by default UTF-8 will be used.
Syntax – string.encode(encoding= encoding , errors= errors)
Parameter values –
encoding – Encoding type of string has to be encoded.error – Works if encoding gets fail.
Six types of errors which works with encoding –
- strict – By default it is set with UTF-8 otherwise it raises an error on failure.
- ignore – It ignores the unencodable character from the output.
- replace – It replaces unencodable characters in the question mark (?).
- xmlcharrefreplace – It replaces character in the XML character.
- backslashreplace – It uses backslash instead of unencoded (couldn’t be encoded) character.
- namereplace – It replaces unencodable unicode character in text format.
Example: By default encoded with UTF-8.
String = 'Hello, Pythøñ'
text = String.encode()
print("before encoding:", String)
print("after encoding:", text)
#output:
before encoding: Hello, Pythøñ
after encoding: b'Hello, Pyth\xc3\xb8\xc3\xb1'
Example: Use of encoded with an error.
String = 'Hello, Pythøñ'
text = String.encode('ascii', 'ignore')
txt = String.encode('ascii', 'replace')
print(String)
print(text)
print(txt)
#output:
Hello, Pythøñ
b'Hello, Pyth'
b'Hello, Pyth??'
Example: Use of encoded with an error.
String = 'Hello, Pythøñ'
text = String.encode('ascii', 'ignore')
txt = String.encode('ascii', 'namereplace')
print(String)
print(text)
print(txt)
#output:
Hello, Pythøñ
b'Hello, Pyth'
b'Hello, Pyth\\N{LATIN SMALL LETTER O WITH STROKE}\\N{LATIN SMALL LETTER N WITH TILDE}'
Conclusion
By understanding and utilizing these Python string methods, you can enhance your string manipulation and processing capabilities, leading to more efficient and robust code. Whether it’s case-insensitive comparisons, formatting adjustments, suffix checks, or character encoding, these methods offer valuable functionality for your Python string operations.