A Python String can be understood as a collection of characters. It is a compound data type made-up of individual characters. These characters can be alphabets, numbers or special characters on the keyboard. In this article you will understand Python Strings in detail and how you can manipulate them with functions and operations.
Python String Format
Python String is specified by a set of characters enclosed within double quotes (“”). The format for creating a variable of string data type is
variableName= “characters-of-the-string”
Example
Vowels=”aeiou”
Consonants=”bcdfghjklmnpqrstvwxyz”
Accessing Python String
A string can be used as a single value. A string can be printing by passing it as argument in Python print function. To access individual characters or elements of a string you can use index of that character in square parenthesis.
Example
print(Vowels[2]) # Python String prints “i” as output since the indices of characters in string start with 0.
Length of a String
Sometimes it may be very essential to know the length of a string for a specific programming task. Python len function takes a string as argument and returns the number of characters in it.
Syntax of len function is
len(String)
Example
len(Vowels) #returns 5
Comparing Strings
Strings in Python can be compared by using the comparison operators. These operators are greater than(>), greater than equal to(>=), less than(<), less than equal to(<=), equal to(==) and not equal to (!=). Any of these comparison operators can be placed between two strings or string variables to compare them.
Example
Str1="Hello"
Str2="World"
print(Str1==Str2)
print(Str1!=Str2)
print(Str1>Str2)
print(Str1>=Str2)
print(Str1<Str2)
print(Str1<=Str2)

String Slices
String segments can be extracted from an existing string. Slicing is like extracting the set of characters by specifying the index range separated by colon “:” in square parenthesis.
Syntax
StringName[x:y]
The string name is followed by square parenthesis, x represents the start index of the slice and y is the end index of the slice that you want to extract. If first index is not provided the segment starts from index 0. If end index is not provided the segment ends at the last index. If both indices are not provided the whole string is returned as a segment
Example
Str1="Twinkle Twinkle Little Star"
print(Str1[:]) #prints complete string as segment
print(Str1[4:20]) #prints segment from index 4 to 20
print(Str1[:16]) #prints segment from 0 to 16
print(Str1[7:]) #prints segment from 7 to last index

Strings are Immutable
A String variable can be updated with a new string. One or more characters cannot be modified in a string. This property of Python string defines that Python strings are immutable.
Example
Str1="Twinkle Twinkle Little Star"
print(Str1)
Str1[5]='X'
Str1='Humpty Dumpty'
print(Str1)

Str1="Twinkle Twinkle Little Star"
print(Str1)
Str1='Humpty Dumpty'
print(Str1)

There are many other operations available in string module. Explore it here
Be First to Comment