slices to the rescue :)

def left(s, amount):
    return s[:amount]

def right(s, amount):
    return s[-amount:]

def mid(s, offset, amount):
    return s[offset:offset+amount]
Answer from Andy W on Stack Overflow
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-stack-overflow
Python Stack Overflow: A Comprehensive Guide - CodeRivers
February 22, 2026 - In the world of Python programming, the concept of stack overflow is both important and potentially tricky. A stack overflow occurs when the call stack, which stores information about function calls and local variables, runs out of space. This can happen due to various reasons, such as infinite ...
Top answer
1 of 2
9

You'd mask the resulting value, with & bitwise AND:

mask = 2 ** 16 - 1
k = (i << j) & mask

Here 16 is your desired bit width; you could use i.bit_length() to limit it to the minimum required size of i, but that'd mean that any left shift would drop bits.

The mask forms a series of 1 bits the same width as the original value; the & operation sets any bits to 0 outside of these:

>>> 0b1010 & 0b111
2
>>> format(0b1010 & 0b111, '04b')
'0010'

Some side notes:

  • You are left shifting, not right shifting.
  • You appear to have forgotten to a d in your debug print:

    print "%d left shift %d gives" % (i,j)
    

    There was a lone % there that combined with the g for gives to make %g (floating point formatting).

  • You can use:

    def showbits(x):
        return format(x, '016b')
    

    to format an integer to a 0-padded 16-character wide binary representation without the 0b prefix.

2 of 2
0

Because Python does some magic to prevent this condition, known as an overflow, from happening. It does this by adjusting the type:

>>> i = 5225
>>> type(i)
<type 'int'>
>>> j = i << 16; type(j); bin(j)
<type 'int'>
'0b10100011010010000000000000000'
>>> j = i << 32; type(j); bin(j)
<type 'int'>
'0b101000110100100000000000000000000000000000000'
>>> j = i << 64; type(j); bin(j)
<type 'long'>
'0b10100011010010000000000000000000000000000000000000000000000000000000000000000'
>>> j = i << 128; type(j); bin(j)
<type 'long'>
'0b101000110100100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

If you want to limit the bits you want to work with, do as Martijn suggests and use a 16-bit mask.

>>> j = 0xffff & (i << 12); type(j); bin(j)[2:].zfill(16)
<type 'int'>
'1001000000000000'
>>> j = 0xffff & (i << 13); type(j); bin(j)[2:].zfill(16)
<type 'int'>
'0010000000000000'
>>> j = 0xffff & (i << 14); type(j); bin(j)[2:].zfill(16)
<type 'int'>
'0100000000000000'
>>> j = 0xffff & (i << 15); type(j); bin(j)[2:].zfill(16)
<type 'int'>
'1000000000000000'
>>> j = 0xffff & (i << 16); type(j); bin(j)[2:].zfill(16)
<type 'int'>
'0000000000000000'
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71826536 โ€บ python-strip-function-works-only-on-the-left-side-of-a-string
Python strip function works only on the left side of a string - Stack Overflow
Explore Stack Internal ... I have a file with text lines that I want to strip the first and last characters and then store the stripped lines in a list. The text is something like: %5 sdgfjfhlsjf %5 %5 alkdregtrlkjdls %5 %5 dgglssj %5 ยท In order to get rid of the leading and trailing characters (%5), I used the strip function in a few variants of code, but I didn't get the expected result.
Find elsewhere
Top answer
1 of 3
2

this code will work. I assume you already have a function got getting links. I have just used a dummy one _get_links. You will have to delete the content of links file and need to put 0 in index file after every successful run.

import time

def _get_links():
    return ["a", "b", "c"]

def _get_links_from_file():
    with open("links") as file:
        return file.read().split(",")


def _do_something(link):
    print(link)
    time.sleep(30)

def _save_links_to_file(links):
    with open("links", "w") as file:
        file.write(",".join(links))
    print("links saved")

def _save_index_to_file(index):
    with open("index", "w") as file:
        file.write(str(index))
    print("index saved")

def _get_index_from_file():
    with open("index",) as file:
        return int(file.read().strip())


def process_links():
    links=_get_links_from_file()
    if len(links) == 0:
        links = _get_links()
        _save_links_to_file(links)
    else:
        links = _get_links_from_file()[_get_index_from_file():]


    for index, link in enumerate(links):
        _do_something(link)
        _save_index_to_file(index+1)

if __name__ == '__main__':
    process_links()
2 of 3
1

I would suggest that you write out the links to a file along with a date/time stamp of the last time it was processed. When you write links to the file, you will want to make sure that you don't write the same link twice. You will also want to date/time stamp a link after you are done processing it.

Once you have this list, when the script is started you read the entire list and start processing links that haven't been processed in X days (or whatever your criteria is).

Steps:

  1. Load links file
  2. Scrape links from sitemap, compare to existing links from file, write any new links to file
  3. Find the first link that hasn't been processed in X days
  4. Process that link then write date/time stamp next to link, e.g.

    http://www.google.com,1/25/2019 12:00PM
    
  5. Go back to Step 3

Now any time you kill the run, the process will pick up where you left off.

NOTE: Just writing out the date may be enough. It just depends on how often you want to refresh your list (hourly, etc.) or if you want that much detail.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ stack-in-python
Stack in Python - GeeksforGeeks
Stack is a linear data structure that follows the Last-In/First-Out (LIFO) principle. ... Python does not have a built-in stack type, but stacks can be implemented using different data structures.
Published ย  May 19, 2026
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_ljust.asp
Python String ljust() Method
Note: In the result, there are actually 14 whitespaces to the right of the word banana. The ljust() method will left align the string, using a specified character (space is default) as the fill character.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ stack-overflow-python
Demystifying Stack Overflow in Python - CodeRivers
February 22, 2026 - In the world of Python programming, encountering a stack overflow error can be both frustrating and confusing. A stack overflow occurs when the call stack, which stores information about function calls and local variables, runs out of space. This blog post aims to provide a comprehensive understanding of stack overflow in Python, including its fundamental concepts, how to recognize it, common causes, and best practices to avoid it.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-right-and-left-shift-characters-in-string
Python - Right and Left Shift characters in String - GeeksforGeeks
July 12, 2025 - s = "geeksforgeeks" k = 17 n = len(s) k %= n l = s[k:] + s[:k] r = s[-k:] + s[:-k] print("Left Shift:", l) print("Right Shift:", r) ... k %= n reduces unnecessary full rotations. Shifting a 13-character string by 17 is the same as shifting by 4 (since 17 % 13 = 4). Slicing is then used to perform the shifts. Deque (double-ended queue) from the collections module is designed for efficient rotations and manipulations. This method uses the built-in rotate function of deque for shifting characters.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.6 documentation
Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state). sort() accepts two arguments that can only be passed by keyword (keyword-only arguments): key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower).
๐ŸŒ
Python
python.org โ€บ dev โ€บ peps โ€บ pep-0651
PEP 651 -- Robust Stack Overflow Handling | Python.org
January 18, 2021 - It will return -1 and set an exception, if the C stack is near to overflowing. The where parameter is used in the exception message, in the same fashion as the where parameter of Py_EnterRecursiveCall(). Py_EnterRecursiveCall() will be modified to call Py_CheckStackDepth() before performing its current function.
๐ŸŒ
ListenData
listendata.com โ€บ home โ€บ python
String Functions in Python with Examples
The table below shows many common string functions in Python along with their descriptions and their equivalent functions in MS Excel. If you are intermediate MS Excel users, you must have used LEFT, RIGHT and MID Functions.