Index and slice Strings in Python

21-12-22 Ahmed Obaid 2729 0

​A string in Python is a sequence of one or more characters and can consist of letters, numbers, white spaces (blank), or symbols. Indexing in Python allows us to access specific characters in a given string using a numeric value. String indexing starts at zero where the first character in the string has index number 0, the next character has index number 1, and so on.

index-and-slice-strings-in-python-2

Note the previous figure, the string begins at the letter A corresponding to index number 0, and the string ends at the symbol ! which corresponds to index number 11.

Note also that the (empty) whitespace has an index number of 5. And also the character symbol ! It has index number 11

There are two ways to access characters in a string:


  • By the positive index number starting from number 0

  • By negative index number starting with -1

Access characters by positive index number

In this type of indexing, we pass the positive index (the one we want to access) in square brackets. The index number starts at index number 0 (which indicates the first character of the string).

Example:

# accessing the character of str at 0th index
string = "Ahmed Obaid!"
print(string[0])

The output will be:

A

 In the previous example, when we referred to a specific index number in the string, Python returned the character at that position. Since the letter A is at index 0 of the string "!Ahmed Obaid", when we printed string[0] the output was A .

 example:

# accessing the character of str at 10th index from the last
string = "Ahmed Obaid!"
print(string[-10])

 The output will be:

b

 In the previous example, when we referred to a specific index number in the string, Python returned the character at that position. Since the character b is at index 7 of the string "!Ahmed Obaid", when we printed string[7] the output was b .

 Access characters by negative index number

If we have a long string and we want to select an element at the end, we can count down from the end of the string, starting with index number -1 (which refers to the last character of the string). Then we pass the negative index (the one we want to access) in square brackets.

 Example :

# accessing the character of str at last index
string = "Ahmed Obaid!"
print(string[-1])

 The output will be:

!

 In the previous example when we referred to a specific index number in undefined String, Python returns the character at this position. Since the symbol ! It is located at the end of the string at negative index number -1 of the string "!Ahmed Obaid". When we printed string[-1], the output was ! .

 Another example:

# accessing the character of str at 10th index from the last
string = "Ahmed Obaid!"
print(string[-10])

 The output will be:

m

 In the previous example, when we referred to a specific index number in the string, Python returned the character at that position. Since the character m is at negative index -10 of the string "!Ahmed Obaid", when we printed string[-10] the output was b .

 Slicing of chains

 Slicing in Python is a feature that enables us to access parts of a string. We use slashing when we order a part of the chain, not the whole chain. Suppose we want to print only the word Obaid from the string "!Ahmed Obaid" We can do this with a range of index numbers separated by a colon [start : end : step].

 Build Indexing Range [start : end : step]


  •  start: means starting index from here (  the place you start from )

  •  end: means the end of the index here (where you end)

  •  step : is an optional argument that specifies the increment between each index

Example:

# declaring the string
str = "Ahmed Obaid!"
#Print only the word Obaid from the string
print(str[6:11])

 The output will be:

Obaid

 In the previous example, we just typed the word Obaid from the string "Ahmed Obaid!"

 If we want to include the beginning or end of the string in the trimmed part of the string, we can do that by not putting a number for the beginning of the index or a number for the end of the index. For example, if we want to print only the word ahmed from the string, we do that by writing print(str[:5]) as shown in Next example

 Example:

# declaring the string
str = "Ahmed Obaid!"
#Print only the word Ahmed from the string
print(str[:5])

 The output will be:

Ahmed

 If we want to truncate a part of the string starting from index number 6 to the end of the string, we do as in the following example

 Example:

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[6:])

  The output will be:

Obaid!

 You can also use negative indexes to split a string, as mentioned earlier, the numbers of negative indexes start from undefined The number -1, and the countdown continues until we reach the beginning of the string and when using negative indexes we will start from the smallest number because it is located first in the string.

 Example: 

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[-12:-7])

 The output will be:

Ahmed

 Note: While using negative numbers, make sure not to pass an index less than - (the length of the string). Otherwise, your program will encounter an error.

 By default, the third parameter, step, is set to 1. It specifies the increment between each index and indicates the number of characters to exceed in the text string. As shown in the following example

 Example

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[6:11:1])

 The output will be:

Obaid

 As in the previous example, if step parameter is 1, all characters between the two indexes will be included. If the step parameter is not mentioned from the ground up, Python automatically adds one to the index range, but if we change the value of the step parameter to another number, some characters will be ignored.

 Example :

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[6:11:2])

 The output will be:

Oad

 In the previous example, we changed the value of the step parameter to 2, which led to skipping a character between every two characters

 You can also specify a negative number as the step parameter so that the string will be ordered in reverse

 Example :

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[-2:-11:-8])

 The output will be:

dm

 You can delete the two indexes and leave the colon ( : ) This will keep the entire string.

 Example:

# declaring the string
str = "Ahmed Obaid!"
# slicing using indexing sequence
print(str[:])

 The output will be:

Ahmed Obaid!

 External sources for more information:

 Strings: The official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python python data types Python String Index and slice strings

Share page