Python Strings

In Python programming, a string is a sequence of Unicode characters.

The technique of converting a character to a number is called encoding, and the reverse process is called decoding.

ASCII and Unicode are some of the popular encoding techniques being used.

Unicode was introduced in computer science to include every character in all languages and bring uniformity in encoding. 

Python Strings can be created using single quotes or double quotes or even triple quotes.

The following example shows this: 

# all of the following strings are equivalent
string = 'Hello World'
print(string)

string = "Hello World"
print(string)

string = '''Hello World'''
print(string)

# triple quotes string can extend multiple lines
string = """Hello everyone, welcome to
the world of Python programming"""
print(string)

Output:

Hello World
Hello World
Hello World
Hello everyone, welcome to
the world of Python programming

 

Strings as arrays

Like some other popular programming languages (e.g. Java), strings in Python are arrays of bytes representing Unicode characters.

However, Python language does not have a character type, a single character is basically a string of length one. Square brackets “[ ]” can be used to access elements of the string.

See the example below: 

# Accessing elements of a string
str = "My country is beautiful!"

# gets the element at position 3
print(str[3])

# gets the characters from position 3 to position 7 (not taken)
print(str[3:7])

# removes the white spaces at the beginning and end
temp = " Hello, Information Technology! "
print(temp.strip())

# returns the length of a string
print(len(str))

# returns the string in lower case
print(str.lower())

# returns the string in upper case
print(str.upper())

# replaces a string with another string
print(str.replace("c", "C"))

# splits the string into sub-strings if it finds instances of the separator
print(str.split(" "))

Output:

c
coun
Hello, Information Technology!
24
my country is beautiful!
MY COUNTRY IS BEAUTIFUL!
My Country is beautiful!
['My', 'country', 'is', 'beautiful!']

 

String Formatting

Python does not allow combining strings and numbers using ‘+’ operator (generates TypeError). To do this we have to use the format() method.

This method takes the passed arguments, formats them, and places them in the string where the placeholders { } are. See the code below:

# Combining strings and numbers using '+' operator not allowed
num = 8.55
var = "My grade is :"
# print(var + num) # TypeError: must be str, not float

# Combine string and number using format() and placeholder { }
num = 8.55
str = "My grade is {}"
print(str.format(num))

# format() takes unlimited number of arguments
roll = 21
year = 3
grade = 8.25
string = "My roll number is {}, year {}rd and grade {}."
print(string.format(roll, year, grade))

# format() using index numbers
age = 32
salary = 50
id = 101
stmt = "I have id {2}, my salary is {1}k per month and age {0}."
print(stmt.format(age, salary, id))

# format() using named indexes
txt = "The capital of {county} is {city}."
print(txt.format(county = "India", city = "Delhi"))

Output:

My grade is 8.55
My roll number is 21, year 3rd and grade 8.25.
I have id 101, my salary is 50k per month and age 32.
The capital of India is Delhi.