Menu Close

Methods strip() and format()

While working with Python strings, there can often be the need to remove the leading and trailing characters of a string or create strings with values that can be modified afterwards. We are able to achieve this through the use of the “strip()” and “format()” methods for strings.

strip() method

In python, the strip method is used to remove leading and trailing characters of a string. The syntax for the strip() method is as follows:

strip(set of characters)

When the character set is blank, it removes leading and trailing spaces by default.

Example 1:

myString = (“                           python is fun!                     “)
newString = myString.split()

print(newString)

Output:

python is fun!

The code above removes the leading and trailing spaces of the string, leaving only the desired information. This can be useful in situations where the user may not have intended to put a leading or trailing space. For example, in a registration form, many people will accidentally type a space at the end due to force of habit, or there will be one at the beginning and the user fails to realize it. In these cases, it is almost certain that the space was not intended, and subsequently should be removed.

Example 2:

myString = “ababababbbaaaabbbaaaaPineappleabaabbbababbabababa”
newString = myString.split(“ab”)

print(newString)

Output:

Pineapple

In this case, all As and Bs were deleted because of the split function. They were passed in through the parenthesis.

Example 3:

myString = “ababababbbaaaabbbaaaaPineappleabaabbbababbabababa”
newString = myString.split(“aabb”)

print(newString)

Output:

Pineapple

In this case, the split function ignores repeated letters, so the output is the same as in example 2.

Example 4:

myString = “eeeeeeeeeeeeeeeeeeeeePineappleeeeeeeeeeeeeee”
newString = myString.split(“e”)

print(newString)

Output:

Pineappl

In this case, it is important to remember to be careful when the leading or trailing character is the same as the one you are attempting to remove.

format() method

Often in Python, we need strings that are easily changeable with different variables. For this, we use the “format()” method in Python, which replaces parts of a string with variables. To use the method, the string must have “{value}”. For example:

string1 = “This is my string about {value}”

You can leave the value blank as the value is not needed.

Syntax:

To work with these strings, we use the syntax as follows:

string.format(value=item)

Example 1:

string1 = “I want to format this string {}”
print(string1.format(“today”))

Output:

I want to format this string today

In this case, the value in the braces was left empty.

Example 2:

string1 = “I want to format this {date}, using {language}”
print(string1.format(language=”python”, date=”today”)

Output:

I want to format this today, using python

In the case above, arguments are passed in and used to create changeable strings

F-strings

*This only works for python 3 and above

In the newer versions of python, we are able to do away with the format() method, and we can implement these directly into the string itself.
This is what the syntax for an f-string is.

String = f”{Variable} rest of the string”

This directly accesses the variable mentioned inside the f-string itself, and it shortens and simplifies code drastically.

Example:

newVariable = “Python”
newString = “{newVariable} is cool!”

print(newString)

Output:

Python is cool!
Posted in Embedded Programming Language, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!