python-string-method

Python String Method Casefold(), Endswith(), Encode()

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

Introduction – Python is open-source and free programming language. It is easy to use and learn as compared to other languages like C ++, Java, C# and JavaScript. It is highly recommended and allows itself to get integrated with other languages like C ++, C#, etc., One of the most important key factors of Python is it is Object-Oriented Language and had been used by many giant companies for so many purposes such as handling file data, web app, Desktop application, Machine learning and so on.

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)
python-string-method
As it is shown clearly that casefold() method converted all letters of string into lower case.

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)
python-string-method

Example 2- Execution of center() method with default fillchar.

x = 'Python in Python'
z = x.center(17)

print(z)
python-string-method
As it is shown clearly that variable is surrounded from left and right side with default space as fillchar.

Example 3- Execution of center() method with fillchar.

x = 'Python in Python'
z = x.center(50, '*')

print(z)
python-string-method
As it is shown clearly that variable is surrounded from left and right side with sign (*) as fillchar.

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)
python-string-method
As it is shown clearly that sentence is finished with suffix (!) exclamation mark therefore, it showed True.

Example 2- Check if sentence is not finished with exclamation mark (!).

x = "python in python"
z = x.endswith(!)

print(z)
python-string-method
As it is shown clearly that sentence is not finished with exclamation mark. Therefore, it showed False.

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)
python-string-method
As it is shown clearly that sentenced finished with suffix (‘programming language’) therefore, it showed True.

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)
python-string-method
As it is shown clearly that sentenced finished with suffix (‘programming language’) between the range therefore, it showed True.

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)
python-string-method
As it is shown clearly that sentence is finished with suffix as tuple, therefore it shown True.

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)
python-string-method
As it is shown clearly that sentence is not finished with suffix as tuple, therefore it shown False.

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)
python-string-method
As it is shown clearly that variable have encoded into encoded version.

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)
python-string-method
As it is shown clearly that variable have encoded into encoded version.

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)
python-string-method
As it is shown clearly that variable have encoded into encoded version.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on