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
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_string.asp
Python String Methods
Python has a set of built-in methods that you can use on strings.
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_lstrip.asp
Python String lstrip() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
InterviewQs
interviewqs.com โ€บ ddi-code-snippets โ€บ substring-python
Slice a string in python (right, left, mid equivalents) - InterviewQs
A step-by-step Python code example that shows how to slice a string (right, left, mid equivalents). Provided by InterviewQs, a mailing list for coding and data interview problems.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-right-and-left-shift-characters-in-string
Python - Right and Left Shift characters in String - GeeksforGeeks
April 16, 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.
๐ŸŒ
ListenData
listendata.com โ€บ home โ€บ python
String Functions in Python with Examples
This tutorial explains various string (character) functions used in Python, with examples.
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python strings
Python Strings | Python Education | Google for Developers
The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point), and the matching values in a tuple on the right (a tuple is made of values separated by commas, typically grouped inside parentheses):
Find elsewhere
๐ŸŒ
Trymito
trymito.io โ€บ excel-to-python โ€บ functions โ€บ text โ€บ LEFT
Excel to Python: LEFT Function - A Complete Guide | Mito
Excel's LEFT function allows you to extract a specific number of characters from the start of a text string. This is commonly used in data preparation tasks such as splitting columns, extracting codes, or cleaning up data.
๐ŸŒ
Real Python
realpython.com โ€บ python-strings
Strings and Character Data in Python โ€“ Real Python
December 22, 2024 - In this tutorial, you'll learn how to use Python's rich set of operators and functions for working with strings. You'll cover the basics of creating strings using literals and the str() function, applying string methods, using operators and built-in functions with strings, and more!
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ left string in python
How to Get Parts of a String in Python | Delft Stack
February 15, 2024 - If we decrease the value, such as -5, we will get the fifth final element of the original string. Now, letโ€™s use the step as a parameter in our slicing method. In this example, we get the elements by adding step = 2 in the program. As shown below, we get the elements with a gap of 1 element. # python originalString = "Slicing the string with Python" stepString = originalString[::2] print("The string we collected by using the step is: ", stepString)
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_strip.asp
Python String strip() Method
Leading means at the beginning of the string, trailing means at the end. You can specify which character(s) to remove, if not, any whitespaces will be removed. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_rsplit.asp
Python String rsplit() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
W3Schools
w3schools.com โ€บ asp โ€บ func_left.asp
VBScript Left Function
The Left function returns a specified number of characters from the left side of a string.
๐ŸŒ
Databricks
docs.databricks.com โ€บ pyspark reference โ€บ functions โ€บ left
left | Databricks on AWS
June 4, 2026 - left function in PySpark: Returns the leftmost len(len can be string type) characters from the string str, if len is less or equal than 0 the result is an empty string.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - Notice that the start or end index can be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (from the right to left).
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
If it is an integer, it represents the index of the positional argument in args; if it is a string, then it represents a named argument in kwargs. The args parameter is set to the list of positional arguments to vformat(), and the kwargs parameter is set to the dictionary of keyword arguments. For compound field names, these functions are only called for the first component of the field name; subsequent components are handled through normal attribute and indexing operations.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings.asp
Python Strings
Learn more about For Loops in our Python For Loops chapter. To get the length of a string, use the len() function.
๐ŸŒ
W3Schools
w3schoolsua.github.io โ€บ python โ€บ python_strings_methods_en.html
Python - String Methods. Lessons for beginners. W3Schools in English
Python - String Methods. Python has many built-in methods that you can use on strings. Examples. Brief description of the method. Examples. Table. Lessons for beginners. W3Schools in English
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-string.html
Python Strings
Python strings are written between ... pretty much every language on earth, plus many emojis. See the unicode section below for more information. The len() function ......