Python Strings

There are three major sequences in python. Strings, Lists and Tuples.

A sequence is a iterable(can be indexed) object in python which contains sequence of values. String is the most simple sequence type, it is a combination of one or multiple characters or numbers together.

Strings can be created in python in three ways shown below. Special mention to triple quoted strings. Triple quotes are used when the string itself has quotes inside it. Once created, all these three styles of strings have the same properties.

Sample Output

How to create strings in python
How to create strings in python


String Indexing

You can access parts of a string using indexes. Remember python indexes starts from zero!

Sample Output

You can access parts of strings using indexes
You can access parts of strings using indexes


Strings are Immutable

Once you create a python string, you cannot change parts of it! This is known as Immutable property of strings. You can overwrite it completely but cannot alter parts of it.

This is the same reason why strings are a fast data structure, because python don’t have to maintain the change metadata for the string variables.

Consider below scenario where we are trying to change a part of the string, it will throw error.

Sample Output

Python Strings are immutable
Python Strings are immutable


Common string commands

Once you create any object in python it ‘inherits’ many functions and attributes from the parent class. you can see all of them by adding a dot to the variable name and hit the tab button.

You can see the inherited functions for any variable in python using the tab button after a dot
You can see the inherited functions for any variable in python using the tab button after a dot

Out of the long list, there are some string commands which is very commonly used. Listing them below.

Sample Output

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *