onlinecode

Removing the first n characters of a string in Python

Removing the first n characters of a string in Python

In this post we will give you information about Removing the first n characters of a string in Python. Hear we will give you detail about Removing the first n characters of a string in PythonAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to remove the first n characters of a string in Python.

consider, we have a following string:

str = "good morning"

Now, we want to remove the first 3 characters goo from the above string.

Removing the first n characters

To remove the first n characters of a string we can use the slice notation [ ] by passing n: as an argument.

n is the number of characters we need to remove the from the beginning of a string.

Here is an example, that removes the first 3 characters from the following string.

str = "good morning"modified = str[3:]print(modified)

Output:

"d morning"

Alternatively, we can also use the lstrip() method by passing the first n characters as an argument to it.

str = "good morning"modified = str.lstrip("goo")print(modified) # "d morning"

Another example of lstrip() method:

name = "pearson"modified = name.lstrip("pear") # removing first 4 charactersprint(modified) # "son"

Hope this code and post will helped you for implement Removing the first n characters of a string in Python. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version