The slicing of s is non-inclusive.

s[start:end]

Meaning the output is up to but not including the end value. This means that the line

print (s[i:i+1])

is equivalent to

print (s[i])

According to this reference (https://blog.finxter.com/daily-python-puzzle-overshoot-index-slicing/)

A little-known feature of slicing is that it has robust end indices. Slicing is robust even if the end index is greater than the maximal sequence index. The slice just takes all elements up to the maximal element. If the start index is out of bounds as well, it returns the empty slice.

The follow code gives

s ="123"

roman = {"I":1, "IV":4, "V":5, "IX":9, "X":10, "XL":40, "L":50, 
                "XC":90, "C":100, "CD":400, "D":500, "CM":900, "M":1000}
out = 0
n = len(s)
i = 0

while i < n:
    
    print (s[i:i+2])
    
    
    i = i + 1

print(s[5:7])

gives:

12
23
3

Answer from Klimunmm on Stack Overflow
🌐
Stanford CS
cs.stanford.edu β€Ί people β€Ί nick β€Ί py β€Ί python-range.html
Python range() Function
The most common form is range(n), given integer n returns a numeric series starting with 0 and extending up to but not including n, e.g. range(6) returns 0, 1, 2, 3, 4, 5. With Python's zero-based indexing, the contents of a string length 6, are at index numbers 0..5, so range(6) will produce ...
Discussions

Not really understanding when to use for I in string vs for I in range(len(string))
For strings, there is basically zero reason to use the latter. If you need access to the index, use enumerate. For lists, you use the latter when you are changing the list that you're iterating over. More on reddit.com
🌐 r/learnpython
14
8
September 27, 2020
loops - Using range function in Python - Stack Overflow
I am new to learning Python and have a question about using the range function to iterate a string. Let's say I need to capitalize everything in the following string: string = 'a b c d e f g' Can I... More on stackoverflow.com
🌐 stackoverflow.com
Convert range(r) to list of strings of length 2 in python - Stack Overflow
I just want to change a list (that I make using range(r)) to a list of strings, but if the length of the string is 1, tack a 0 on the front. I know how to turn the list into strings using ranger=... More on stackoverflow.com
🌐 stackoverflow.com
python range and string - Stack Overflow
What it does is take a list and turn it into a string by putting a space between each of the items. You would think that it'd be a function you call on the list, but in Python it's a function on the string instead. ... numbers = range(1, n) make_count_string = lambda x: "%d one thousand." More on stackoverflow.com
🌐 stackoverflow.com
July 28, 2015
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_func_range.asp
Python range() Function
Remove List Duplicates Reverse ... Training ... The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number....
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί not really understanding when to use for i in string vs for i in range(len(string))
r/learnpython on Reddit: Not really understanding when to use for I in string vs for I in range(len(string))
September 27, 2020 -

Just looking for clarification for this very simple thing. Please correct me if I’m wrong but from what I understand, in for i in string, it takes each element in the string over the entire length. For i in range(len(string)) it indexes the elements and looks at the elements at each index so at position 0, string =β€˜x’ and at 1 string= β€˜d’ ect.

If this is correct, I’m afraid I still don’t see the difference by way of when to use each or what purpose they serve.

🌐
Quora
quora.com β€Ί How-do-you-access-a-range-of-characters-from-a-string-in-Python
How to access a range of characters from a string in Python - Quora
Answer (1 of 3): Python – Extract range characters from String Given a String, extract characters only which lie between given letters. > Input : test_str = β€˜geekforgeeks is best’, strt, end = β€œg”, β€œs” Output : gkorgksiss Explanation : All characters after g and before s are retained.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-convert-string-ranges-to-list
Python | Convert String ranges to list | GeeksforGeeks
May 16, 2023 - In this, the split is performed on hyphens and comma and accordingly range, numbers are extracted and compiled into a list. ... # Python3 code to demonstrate working of # Convert String ranges to list # Using sum() + list comprehension + enumerate() + split() # initializing string test_str = &quot;1, 4-6, 8-10, 11&quot; # printing original string print(&quot;The original string is : &quot; + test_str) # Convert String ranges to list # Using sum() + list comprehension + enumerate() + split() res = sum(((list(range(*[int(b) + c for c, b in enumerate(a.split('-'))])) if '-' in a else [int(a)]) for a in test_str.split(', ')), []) # printing result print(&quot;List after conversion from string : &quot; + str(res))
Find elsewhere
🌐
AskPython
askpython.com β€Ί python β€Ί string β€Ί string-alphabet-range-python
String - Alphabet Range in Python - AskPython
May 30, 2023 - There are different ASCII values for lowercase and uppercase characters. The lowercase characters range from ASCII values 197-122, and the uppercase characters range from ASCII values 65-90. Python is a great language for working with strings.
🌐
EyeHunts
tutorial.eyehunts.com β€Ί home β€Ί python range to the list of strings | example code
Python range to the list of strings | Example code - EyeHunts
August 27, 2021 - Python simple example code. There is a perfect solution to it -zfill method. Generator for N numbers and fill its string representation with zeros. list1 = [] for i in range(1, 7): list1.append(str(i).zfill(2)) print(list1) Output: lst = range(11) print(["{:02d}".format(x) for x in lst]) Output: [’00’, ’01’, ’02’, ’03’, ’04’, ’05’, ’06’, ’07’, ’08’, ’09’, ’10’] or format: lst = range(11) print([format(x, '02d') for x in lst]) Output: [’00’, ’01’, ’02’, ’03’, ’04’, ’05’, ’06’, ’07’, ’08’, ’09’, ’10’] sr = [] for r in range(11): sr.append('i' % r) print(sr) Do comment if you have any doubts and suggestions on this Python list topic.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-extract-range-characters-from-string
Python - Extract range characters from String - GeeksforGeeks
June 2, 2023 - In this, we check for character in range using comparison operation and list comprehension does task of iteration and creation of new list. Then join() can be employed to reconvert to string. ... # Python3 code to demonstrate working of # Extract range characters from String # Using list comprehension # initializing string test_str = 'geekforgeeks is best' # printing original string print("The original string is : " + str(test_str)) # initializing range letters strt, end = "f", "s" # join() to get result in string res = ''.join([chr for chr in test_str if chr >= strt and chr <= end]) # printing result print("Extracted String : " + str(res))
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 68491454 β€Ί using-range-function-in-python
loops - Using range function in Python - Stack Overflow
Every string in Python ist iterable, so don't use another generator like range. But if your string is "a b c d e f g", every second element is a space " ".
🌐
Real Python
realpython.com β€Ί python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - This means that you can loop directly on the string itself: ... Looping directly on a sequence, like you do here, is simpler and more readable than using indices. If you have a loop where you’re using indices to find individual elements, then you should loop directly on the elements instead. ... Sometimes, you want to work with both indices and the corresponding elements. In the earlier example, you showed the index of each character in a word: ... >>> word = "Loop" >>> for index in range(len(word)): ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-range-duplication-in-string
Python | Range duplication in String - GeeksforGeeks
April 12, 2023 - test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing range i, j = 3, 6 # Range duplication in String # Using string formatting with multiplication temp = "{}{}".format(test_str[i:j], test_str[i:j]) res = "{}{}{}".format(test_str[:i], temp, test_str[j:]) # printing result print("The string after range duplication : " + res) ... Split the string into three parts using split(): the characters before index i, the characters between i and j (inclusive), and the characters after index j. Duplicate the middle part of the string using strin
🌐
EyeHunts
tutorial.eyehunts.com β€Ί home β€Ί python slice string function| get a range of characters (substring)
Python slice string | Get a range of characters (SubString) - EyeHunts
May 18, 2021 - It returns a range of characters(substring). # contains indices (0, 1, 2) obj1 = slice(3) print(obj1) # contains indices (1, 3) obj2 = slice(1, 5, 2) print(slice(1, 5, 2)) ... Answer: You can get python substring by using a split() function or Indexing. ... Answer: The Python split() function breaks up a string at the specified separator space, and returns a list of Strings.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-range-function
Python range() function - GeeksforGeeks
... The range() function only works with integers, i.e. whole numbers. All arguments must be integers. Users can not pass a string or float number or any other type in a start, stop, and step argument of a range().
Published Β  July 11, 2025
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_range.asp
Python range
Remove List Duplicates Reverse ... Training ... The built-in range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times....
🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
September 29, 2025 - Specifying the stride of 4 as the last parameter in the Python syntax ss[0:12:4] prints only every fourth character. Again, let’s look at the characters that are highlighted: ... In this example the whitespace character is skipped as well. Since we are printing the whole string we can omit the two index numbers and keep the two colons within the syntax to achieve the same result: ... Omitting the two index numbers and retaining colons will keep the whole string within range, while adding a final parameter for stride will specify the number of characters to skip.