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.
1 2 3 4 5 6 7 8 |
# Single Quote String print('string1') # Double Quote String print("string2") # Triple Quote String print('''these are some text's with "quotes" inside''') |
Sample Output

String Indexing
You can access parts of a string using indexes. Remember python indexes starts from zero!
1 2 3 4 5 6 7 8 9 10 |
# Accessing a character inside string using index # Python indexes start with 0 print('hello'[1]) # value at second position print('hello'[3]) print('hello'[-1]) # value at last position print('hello'[0:3]) # first three values |
Sample Output

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.
1 2 3 4 5 6 7 |
## STRINGS are NOT Mutable string1="Hello Python" print(string1) print(type(string1)) # string is not mutable, this command produces error string1[1]='a' |
Sample Output

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.

Out of the long list, there are some string commands which is very commonly used. Listing them below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## Common string commands # Defining the string sampleString="This is a string" # finding the length of string print('Length of the string is:',len(sampleString)) # Finding the count of a character print(sampleString.count('i')) # converting to uppercase print(sampleString.upper()) # converting to lowercase print(sampleString.lower()) # splitting into a list of words print(sampleString.split()) # counting the number of words in the string print('number of words:',len(sampleString.split())) |
Sample Output
