Strings in Python

In this article we will discuss about python strings.

What are Strings?

In python, string is a sequence data type that contains letters, numbers, symbols and spaces. Python strings are surrounded by either single quotation marks, or double quotation marks.

greeting = "Hello World!"
morning_greeting = 'Good morning'

Here we have two string variables: greeting, enclosed in double quotation marks, and morning_greeting, enclosed in single quotation marks.

Multiline strings

Using triple quotation mark we can create a multiline string.

patrick_address = """"Patrick A Parry
2316 Jim Rosa Lane
San Francisco, California(CA), 94118"""

brian_address = '''Brian A Ledesma
464 Pine Street
Crafton, Pennsylvania(PA), 15205'''

We have two variables, patrick_address and brian_address, which contain multiline strings with double and single quotation marks respectively.

Python strings are immutable

Strings in Python are immutable, means that the content of a string variable cannot be altered once it is created. Let's discuss this concept with an example.

greeting = "Hello World!"

Here, we have a string variable greeting. Each character of this string variable can be accessed using an index value, similar to how elements are accessed in an array.

greeting = "Hello World!"
print(greeting[0]) # Output: H
print(greeting[3]) # Output: l

Here, we access the characters located at the 0th index and 3rd index, resulting in 'H' and 'l' respectively.

As previously mentioned, strings in Python are immutable, meaning that any attempt to alter a string will result in an error being displayed by the compiler.

greeting = "Hello World!"
greeting[3] = "p" # Output: TypeError: 'str' object does not support item assignment

The above code shows the immutable feature of Python strings.

type() function

The type() function is used to return the type of an object that is passed to it as an argument.

greeting = "Hello World!"
print(type(greeting)) # Output: <class 'str'>

Here, the type() function is passed by greeting variable, so it returns the data type of that variable, which is string .

Find the length of a string

The len() function is used to determine the length of a string.

greeting = "Hello World!"
length_of_greeting = len(greeting)
print(length_of_greeting) # Output: 12

Python string slicing

String slicing in Python is a technique in which we can obtain a substring of the original string by specifying a range of index numbers separated by a colon.

Syntax of String Slicing:

str[start:stop:step]
  • str: Python string to be sliced

  • start: First index of range

  • stop: Last index of range

  • step: No. of step from start index to stop index

Let's understand string slicing with example. Here, we have a variable my_string and it has value programming.

my_string = "programming"

As previously mentioned, we can access each charatcers in Python string using index. The indexing is available in two ways:

  1. Positive indexing

  2. Negative indexing

Here is the pictorial representation of Python string indexing.

Now let’s look at some simple examples of string slicing.

my_string = "programming"

result_1 = my_string[:]
print(result_1) #Output: programming

result_2 = my_string[::]
print(result_2) #Output: programming

result_3 = my_string[0]
print(result_3) #Output: p

result_4 = my_string[4]
print(result_4) #Output: r

result_5 = my_string[:4]
print(result_5) #Output: prog

result_6 = my_string[0:4]
print(result_6) #Output: prog

result_7 = my_string[1:4]
print(result_7) #Output: rog

result_8 = my_string[4:]
print(result_8) #Output: ramming

result_9 = my_string[4:9]
print(result_9) #Output: rammi

result_10 = my_string[4:9:2]
print(result_10) #Output: rmi

result_11 = my_string[-1]
print(result_11) #Output: g

result_12 = my_string[-2]
print(result_12) #Output: n

result_13 = my_string[:-4]
print(result_13) #Output: program

result_14 = my_string[-10:-4]
print(result_14) #Output: rogram

result_15 = my_string[-10:]
print(result_15) #Output: rogramming

result_16 = my_string[-10:-4:2]
print(result_16) #Output: rga

result_17 = my_string[-4:-10:-2]
print(result_17) #Output: mag

result_18 = my_string[::-1]
print(result_18) #Output: gnimmargorp

result_19 = my_string[::-2]
print(result_19) #Output: gimrop

result_20 = my_string[:-5:-1]
print(result_20) #Output: gnim

result_21 = my_string[3::-1]
print(result_21) #Output: gorp

esult_22 = my_string[:3:-1]
print(esult_22) #Output: gnimmar

Python string functions

Python provides many built-in functions to manipulate strings. Since the Python string is immutable, all these functions return a new string, while the original string remains unchanged.