Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Strings Operations (split, strip, replace, count)

User image

Published by

sanya sanya

Published at: 21st May, 2023
2.53 mins read

Introduction

In Python, we represent text data using strings, which are a fundamental data type. In this blog post, we'll talk about some basic Python string operations that can be used to modify strings and extract valuable data from them. Five operations will be covered: divide, strip, replace, and count.

Split

Using a designated delimiter, the split operation divides a string into a list of substrings. Although whitespace is used as the delimiter by default, we can also specify a different delimiter by using the separator argument.

Syntax

https://images.codingblocks.com/data-science/split_syn.png

Here, separator serves as the string's delimiter and maxsplit designates the number of splits that should be made.

Examples

Separating words from a string:

https://images.codingblocks.com/data-science/split_ex_1.png

In the previous example, the sentence was divided into a list of words using the split method.

Using a unique delimiter to split a string:

https://images.codingblocks.com/data-science/split_ex_2.png

In the example above, we divided the date string into three parts using the split operation and the hyphen as the delimiter. Following that, we used multiple assignments to assign the parts to the three variables.

Strip

Remove preceding and trailing whitespace characters from a string using the strip technique. Using the characters argument, we can also specify a particular set of characters to omit.

Syntax

https://images.codingblocks.com/data-science/strip_syn.png

The group of characters to exclude from the string in this case is characters.

Examples:

Getting rid of the leading and trailing spaces

https://images.codingblocks.com/data-science/strip_ex_1.png

To remove the beginning and following whitespace characters from the string in the example above, we used the strip method.

Deleting particular characters:

https://images.codingblocks.com/data-science/strip_ex_2.png?

We removed the trailing period from the string in the aforementioned example using the strip method.

Replace

When using the replace operation, a substring is completely replaced with a different substring.

Syntax:

https://images.codingblocks.com/data-science/replace_syn.png

Old is the substring that needs to be replaced, new is the new substring that needs to be inserted, and count is the maximum number of replacements that can be made.

Examples

Substring substitution:

https://images.codingblocks.com/data-science/replace_ex_1.png

In the previously mentioned illustration, the substring "World" was changed to "Python" using the replace technique.

Limiting the number of replacements:

https://images.codingblocks.com/data-science/replace_ex_2.png

In the previous example, we replaced the first two occurrences of the letter "a" with the letter "x" using the replace operation.

Count

The count operation is used to determine how many times a substring appears in a string.

Syntax

https://images.codingblocks.com/data-science/count_syn.png

In this case, start is the starting index, end is the terminating substring, and substring is the substring to count.

Examples

Counting how many times a substring appears:

https://images.codingblocks.com/data-science/count_ex.png

We have counted the instances of the letter "a" in the substring "babab" in the example above by using the count operator.

Conclusion

In summary, Python's string operations like divide, strip, replace, and count are crucial for modifying and extracting data from strings. These operations allow us to divide strings into smaller chunks, eliminate whitespace and other characters, get rid of or substitute substrings, and count the number of times a substring appears. These operations can make our Python code more effective, readable, and maintainable.

Library

WEB DEVELOPMENT

FAANG QUESTIONS

On this page

Introduction

Split

Strip

Replace

Count

Conclusion