String manipulation

Dealing with strings in Python is very simple: you can search, replace, change character case, and perform other manipulations with ease:

To search for a string, you can use the find method like this:

#!/usr/bin/python3str = "Welcome to Python scripting world"print(str.find("scripting"))

The string count in Python starts from zero too, so the position of the word scripting is at 18.

You can get a specific substring using square brackets like this:

#!/usr/bin/python3str = "Welcome to Python scripting world"print(str[:2]) # Get the first 2 letters (zero based)print(str[2:]) # Start from the second letterprint(str[3:5]) # ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.