Print specific character(s) in a string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 7, 2022

banner

Photo from Unsplash

Print specific character(s) in a string in Python #

Use string slicing to print specific characters in a string, e.g. print(my_str[:5]). The print() function will print the specified character or slice of the string to the terminal.

main.py
my_str = 'bobbyhadz.com' # ✅ print first character in string print(my_str[0]) # 👉️ 'b' # ✅ print second character in string print(my_str[1]) # 👉️ 'o' # ✅ print last character in string print(my_str[-1]) # 👉️ 'm' # ✅ print first 5 characters in string print(my_str[:5]) # 👉️ 'bobby' # ✅ print last 5 characters in string print(my_str[-5:]) # 👉️ 'z.com' # ✅ print each character and its index in string for index, char in enumerate(my_str): print(index, char)

The first 3 examples access a specific character at its index and print the result.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.
main.py
my_str = 'bobbyhadz.com' print(my_str[0]) # 👉️ 'b' print(my_str[1]) # 👉️ 'o' print(my_str[-1]) # 👉️ 'm'

Negative indices can be used to count backwards, e.g. my_str[-1] returns the last character in the string and my_str[-2] returns the second-to-last character.

The print function takes one or more objects and prints them to sys.stdout.

Note that the print() function returns None, so don't try to store the result of calling print in a variable.

Instead, store the expression in a variable and print the result.

main.py
my_str = 'bobbyhadz.com' first = my_str[0] print(first) # 👉️ 'b'

If you try to access an index that is not present in the string, an IndexError is raised.

You can use a try/except statement if you need to handle the error.

main.py
my_str = 'bobbyhadz.com' try: print(my_str[100]) except IndexError: # 👇️ this runs print('Specified index out of range')

If you need to print multiple characters in a string, use string slicing.

main.py
my_str = 'bobbyhadz.com' # ✅ print first 5 characters in string print(my_str[:5]) # 👉️ 'bobby' # ✅ print last 5 characters in string print(my_str[-5:]) # 👉️ 'z.com'

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

If the start index is not specified, the slice starts at index 0.

If the stop index is not specified, the slice starts at the specified index and continues to the end of the string.

If you need to get a slice somewhere in the middle of the string, specify the start and stop indexes.

main.py
my_str = 'bobbyhadz.com' start_index = my_str.index('h') stop_index = my_str.index('.') print(my_str[start_index:stop_index]) # 👉️ hadz

The str.index method returns the index of the first occurrence of the provided substring in the string.

The method raises a ValueError if the substring is not found in the string.

Note that the stop index is exclusive (up to, but not including).

If you need to print the characters in a string and the corresponding index, use the enumerate() function.

main.py
my_str = 'bobbyhadz.com' for index, char in enumerate(my_str): print(index, char) # 👉️ 0 b, 1 o, 2 b, 3 b...

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.