Get characters from the middle of a string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 7, 2022

banner

Photo from Unsplash

Get characters from the middle of a string in Python #

Use string slicing to get characters from the middle of a string, e.g. result = my_str[start_index:stop_index]. The slice of the string will start at the specified index and will go up to, but not including the specified stop index.

main.py
my_str = 'bobbyhadz.com' # ✅ Get characters from the middle of a string start_index = my_str.index('h') stop_index = my_str.index('.') result = my_str[start_index:stop_index] print(result) # 👉️ 'hadz' result = my_str[5:9] print(result) # 👉️ 'hadz' # ----------------------------------------------- # ✅ Get middle character of string def get_middle_char(string): if len(string) == 0: return '' if len(string) % 2 == 0: return string[(len(string) - 1) // 2] else: return string[len(string) // 2] print(get_middle_char('123456')) # 👉️ '3' print(get_middle_char('12345')) # 👉️ '3' print(get_middle_char('1234')) # 👉️ '2' print(get_middle_char('123')) # 👉️ '2' print(get_middle_char('12')) # 👉️ '1' print(get_middle_char('1')) # 👉️ '1'

The first example uses string slicing to extract characters from the middle of a string.

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).

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' start_index = my_str.index('h') stop_index = my_str.index('.') result = my_str[start_index:stop_index] print(result) # 👉️ 'hadz' result = my_str[5:9] print(result) # 👉️ 'hadz'

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

The slice starts at index 5 and goes up to, but not including index 9.

main.py
my_str = 'bobbyhadz.com' print(my_str[2:5]) # 👉️ bby print(my_str[2:7]) # 👉️ bbyha
An easy way to think about it is that the slice starts at the specified index and goes to the given stop index minus 1.

If you need to get the middle character of a string, use the following function.

main.py
def get_middle_char(string): if len(string) == 0: return '' if len(string) % 2 == 0: return string[(len(string) - 1) // 2] else: return string[len(string) // 2] print(get_middle_char('123456')) # 👉️ '3' print(get_middle_char('12345')) # 👉️ '3' print(get_middle_char('1234')) # 👉️ '2' print(get_middle_char('123')) # 👉️ '2' print(get_middle_char('12')) # 👉️ '1' print(get_middle_char('1')) # 👉️ '1'

If the string is empty, we return an empty string.

If the string has an even number of characters, we return the left-middle character.

If the string has an odd number of characters, we return the middle character.

We used the floor division (//) operator because division / of integers yields a float, while floor division // of integers results in an integer.

You can tweak the return values in the function according to your needs.

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.