Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Trimming | String Methods
String Manipulation in Python
course content

Cursusinhoud

String Manipulation in Python

String Manipulation in Python

1. Basic Concepts
2. String Methods
3. Strings Formatting

book
Trimming

As data scientists, we may face some numerical data represented in string format. For example, birth year in format 1991 yr., or weight in format 142 lbs, etc. These units make applying math functions (like mean) impossible.

Fortunately, Python can solve the problem. There is a built-in .strip method that returns a copy of the string by removing both the leading and the trailing characters. If we want to remove elements only on the left side, use .lstrip, and rstrip for symbols on the right side. All these functions by default will remove all the possible space characters.

If we want to delete certain characters, we can set them as an argument in the function. These characters should be written consecutively in one string. For example,

12
print("148 lbs".strip(' lbs')) print("AB string AB".lstrip('AB '))
copy

These methods work the next way: they try to find at least one of the symbols (specified in argument) on the respective side (left for .lstrip, right for .rstrip, and on both sides for .strip) and remove it until no symbol will be found.

Taak

Swipe to start coding

Given list of strings ages containing strings in format ___ y/o. You need to iterate over the list, removing ' y/o' endings, convert each element to an integer type, and calculate the mean.

Do not worry if you are not familiar with some pieces of code.

Oplossing

Note

It's impossible to calculate the mean for the original list since its elements can't be recognized as numbers.

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
toggle bottom row

book
Trimming

As data scientists, we may face some numerical data represented in string format. For example, birth year in format 1991 yr., or weight in format 142 lbs, etc. These units make applying math functions (like mean) impossible.

Fortunately, Python can solve the problem. There is a built-in .strip method that returns a copy of the string by removing both the leading and the trailing characters. If we want to remove elements only on the left side, use .lstrip, and rstrip for symbols on the right side. All these functions by default will remove all the possible space characters.

If we want to delete certain characters, we can set them as an argument in the function. These characters should be written consecutively in one string. For example,

12
print("148 lbs".strip(' lbs')) print("AB string AB".lstrip('AB '))
copy

These methods work the next way: they try to find at least one of the symbols (specified in argument) on the respective side (left for .lstrip, right for .rstrip, and on both sides for .strip) and remove it until no symbol will be found.

Taak

Swipe to start coding

Given list of strings ages containing strings in format ___ y/o. You need to iterate over the list, removing ' y/o' endings, convert each element to an integer type, and calculate the mean.

Do not worry if you are not familiar with some pieces of code.

Oplossing

Note

It's impossible to calculate the mean for the original list since its elements can't be recognized as numbers.

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt