In this article you will learn About Python String Method casefold(), endswith() and encode()
Python String Method – Before moving ahead, let’s know a little bit about Basic Python String
Python String – String in Python are characters which enclosed or surrounded with single or double quotes. String defined as sequence of characters and implemented all of the common sequence operations, along with the additional methods described below.
Now we are moving ahead to know methods of String –
casefold() method – It returns string including all characters in lower case. It is similar to lower() string method but case removes all the case distinctions present in a string.
Syntax - string.casefold() Parameter values - No parameter values.
Example – Converting string in lower case
String = "Hello! How Are You Doing?" h = String.casefold() print(h)
center() method– It returns a string at centered position. Padding is an optional character done with using fillchar. In the absence of fillchar space is taken as default argument. The fillchar is a character which is used to fill left and right of the string.
Syntax - string.center(width[, fillchar]) Parameter values - width - It is required. fillchar - It is optional. Return Type - It returns modified string.
Example 1 – String without center() method.
x = 'Python in Python' print(x)
Example 2- Execution of center() method with default fillchar.
x = 'Python in Python' z = x.center(17) print(z)
Example 3- Execution of center() method with fillchar.
x = 'Python in Python' z = x.center(50, '*') print(z)
endswith() method – It returns True if if a string ends with the specified suffix or specified value. If not, it returns False.
Syntax - string.endswith(suffix[, start[, end]]) or in simply it can be - string.endswith( value, start, end ) Parameter values - value - It is required to check if the string ends with particular suffix. start(Optional) - It is 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) - It is 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 1- Check if sentence is finished with exclamation mark (!).
x = "python in python!" z = x.endswith('!') print(z)
Example 2- Check if sentence is not finished with exclamation mark (!).
x = "python in python" z = x.endswith(!) print(z)
Example 3- Check if the string ends with the phrase “programming language.”.
x = "Python is a programming language" z = x.endswith('programming language') print(z)
Example 4- Check if the string lies between the start and end value.
x = "Python is a programming language" z = x.endswith('programming language', 12, 32) print(z)
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 5- Check if the Tuple ends with particular string.
x = "Python is a programming language" z = x.endswith(('is' , 'language')) print(z)
Example 6- Check if the Tuple does not end with particular string.
x = "Python is a programming language." z = x.endswith(('is' , 'C++')) print(z)
encoded() method – It returns encoded (converted) version of the given string. If no encoded is specified, by default UTF-8 will be used. In other words, string will be converted into given encoded type.
Syntax - string.encode(encoding= encoding , errors= errors) Parameter values - encoding - An encoding type of string has to be encoded. error - It works if encoding gets fail.
There are six types of errors which works with encoding –
1. strict – By default it is set with UTF-8 otherwise it raises an error on failure.
2. ignore – It ignores the unencodable character from the output.
3. replace – It replaces unencodable characters in the question mark (?).
4. xmlcharrefreplace – It replaces character in the XML character.
5. backslashreplace – It uses backslash instead of unencoded (couldn’t be encoded) character.
6. namereplace – It replaces unencodable unicode character in text format.
Note: Try all types of error as well to know how it works.
Example 1- By default encoded with UTF-8.
x = 'Hello, Pythøñ' z = x.encode() print(x) print(z)
Example 2- Use of encoded with an error.
x = 'Hello, Pythøñ' z = x.encode('ascii', 'ignore') y = x.encode('ascii', 'replace') print(x) print(z) print(y)
Example 3 – Use of encoded with error.
x = 'Hello, Pythøñ' z = x.encode('ascii', 'ignore') y = x.encode('ascii', 'namereplace') print(x) print(z) print(y)
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on