Google News
logo
Python Program to Substring. Get the characters from position 2 to position 5 (not included)
In the following example of Python program that demonstrates how to get a substring that contains the characters from position 2 to position 5 (not included) of a string :
Program :
# Substring - Get the characters from position 2 to position 5 (not included)
my_string = "Hello, World!"
print("Original string:", my_string)

substring = my_string[2:5]
print("Substring:", substring)
Output :
Original string: Hello, World!
Substring: llo
In the above program, we start by assigning the string value `"Hello, World!"` to the variable `my_string`. We then use the `print()` function to print the original value of `my_string`.

Next, we use the square bracket notation with the colon (`:`) operator to get a substring that starts at position 2 and ends at position 5 (not included) of `my_string`. We assign the result of this operation to the variable `substring`.

Finally, we use the `print()` function to print the substring.

As you can see, the substring contains the characters from position 2 to position 5 (not included) of the original string, which are `'l'`, `'l'`, and `'o'`.