Python released its latest major release – Python 3.9.0 on October 5th, 2020. The release comes with a number of new features and optimizations including some new string functions. Also, this is the first version of python to default to the 64-bit installer on Windows.
The new release includes a couple of additional string functions removeprefix()
and removesuffix()
to remove prefix and suffix from a string respectively. Let’s look at the syntax and usage of the two functions through some examples –
Python String removeprefix()
The following is the syntax for using the removeprefix()
function:
s.removeprefix(prefix)
Here, s
is the string you want to remove the prefix
from. If the string starts with the prefix, it essentially returns s[len(prefix):]
. If the string does not start with the prefix, the original string is returned. See the example below:
# remove prefix from a string
print('TicTacToe'.removeprefix('Tic'))
print('TicTacToe'.removeprefix('Tac'))
Output:
TacToe
TicTacToe
In the above example, since 'TicTacToe'
starts with the prefix 'Tic'
, the prefix is removed in the string returned by the removeprefix()
function. Whereas, 'TicTacToe'
does not start with the prefix 'Tac'
hence removeprefix()
returns the string as-is.
For more on the function, refer to its official documentation.
Python String removesuffix()
The following is the syntax for using the removesuffix()
function:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
s.removesuffix(suffix)
Here, s
is the string you want to remove the suffix
from. If the string ends with the suffix, it essentially returns s[:-len(suffix)]
. If the string does not end with the suffix, the original string is returned. See the example below:
# remove prefix from a string
print('TicTacToe'.removesuffix('Toe'))
print('TicTacToe'.removesuffix('Tac'))
Output:
TicTac
TicTacToe
In the above example, since 'TicTacToe'
ends with the suffix 'Toe'
, the suffix is removed in the string returned by the removesuffix()
function. Whereas, 'TicTacToe'
does not end with the suffix 'Tac'
hence removesuffix()
returns the string as-is.
For more on the function, refer to its official documentation.
There are also a number of other features and optimizations that have come with this release. Refer to this documentation on what’s new in Python 3.9
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.