
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Index into an Infinite String in Python
Suppose we have a string s and two integers i and j (i < j). Now suppose p is an infinite string of s repeating forever. We have to find the substring of p from indexes [i, j).
So, if the input is like s = "programmer", i = 4, j = 8, then the output will be "ramm".
To solve this, we will follow these steps −
- p:= blank string
- for t in range i to j, do
- p := p concatenate a character from s at index (t mod size of s)
- return p
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s, i, j): p="" for t in range(i,j): p+=s[t%len(s)] return p ob = Solution() s = "programmer" i = 4 j = 8 print(ob.solve(s, i, j))
Input
"programmer", 4, 8
Output
ramm
- Related Articles
- How to prevent loops going into infinite mode in Python?
- How can I represent an infinite number in Python?
- How to stop an infinite loop safely in Python?
- Decoded String at Index in Python
- String Transforms Into Another String in Python
- Index Pairs of a String in Python
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- How we can come out of an infinite loop in Python?
- What keyboard command we have to stop an infinite loop in Python?
- Insert an element into Collection at specified index in C#
- Convert dictionary object into string in Python
- Python Pandas and Numpy - Concatenate multiindex into single index
- Return the lowest index in the string where substring is found using Python index()
- How to create an infinite loop in C#?
- How to run an infinite loop in Tkinter?

Advertisements