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
🌐
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 = 3 n = len(s) k %= n l = ''.join([s[(i + k) % n] for i in range(n)]) r = ''.join([s[(i - k) % n] for i in range(n)]) print("Left Shift:", l) print("Right Shift:", r)
🌐
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.
🌐
Trymito
trymito.io › excel-to-python › functions › text › LEFT
Excel to Python: LEFT Function - A Complete Guide | Mito
In Excel, you would use =LEFT(A1, 2) to extract the first two letters from A1. To achieve the same in pandas, you can use string slicing. The following code extracts the first two characters from the 'text_column' of a DataFrame: ... Often you ...
🌐
Backuptrans
backuptrans.tw › soonix › python-left-string
python left string
November 13, 2024 - my_string = "Python is fun!" # Extract from the first character to the end left_part = my_string[0:] print(left_part) # Output: Python is fun!
🌐
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)
🌐
ListenData
listendata.com › home › python
String Functions in Python with Examples
This tutorial explains various string (character) functions used in Python, with examples. To manipulate strings and character values, python has several built-in functions. 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.
🌐
Tutorialspoint
tutorialspoint.com › python › string_ljust.htm
Python String ljust() Method
Then the remaining blank spaces on the right are filled with the specified character "0" as the fillchar argument using Python String ljust() method. The result is then retrieved: # Initializing the string str = "this is string example....wow!!!"; print (str.ljust(50, '0')) When we run above program, it produces following result: this is string example....wow!!!000000000000000000 · Following is an example where a new string of length 89 is generated and aligns the created string Programming to the left.
Find elsewhere
🌐
Real Python
realpython.com › python-strings
Strings and Character Data in Python – Real Python
December 22, 2024 - In the final example, you use the tabsize argument, which is an optional argument that specifies an alternate tab size. The .ljust(width) call returns a string consisting of the target string left-justified in a field of width characters.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-string.html
Python Strings
Negative numbers also work within [ ] and slices: -1 is the last char in the string, and -2 is the next to last char, and so on. So for example with s = 'Python', s[-1] is 'n' and s[-2] is 'o'. This is convenient when you want to refer chars near the end of the string, and it works in slices too.
🌐
Databricks
docs.databricks.com › pyspark reference › functions › left
left | Databricks on AWS
June 4, 2026 - 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. For the corresponding Databricks SQL function, see left function. Python · from pyspark.sql import functions as dbf dbf.left(str=<str>, len=<len>) Python · df = spark.createDataFrame([("Spark SQL", 3,)], ['a', 'b']) df.select(left(df.a, df.b).alias('r')).collect() On this page · Syntax · Parameters · Examples
🌐
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. Note: All string methods returns new values. They do not change the original string. Note: All string methods returns new values. They do not change the original string. Learn more about strings in our Python Strings Tutorial.
🌐
Codecademy
codecademy.com › docs › python › strings › .rjust()
Python | Strings | .rjust() | Codecademy
October 26, 2023 - The method .rjust() adds padding ... to the left of a given string. The user can specify a character to use as the padding. The number of padding characters added is equal to the length specified in the method call minus the length of the string provided. ... Looking for an introduction to the theory behind programming? Master Python while learning ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python trim functions
How to trim strings in Python - IONOS
January 2, 2025 - The rstrip() method is another way to trim strings in Python. It removes spaces or other char­ac­ters from the end of a string (the right side). It goes through the string from right to left and removes all the relevant char­ac­ters until it en­coun­ters one that shouldn’t be removed. If you use rstrip() without an argument, it will delete all spaces from the end of the string. text = " example " cleaned_text = text.rstrip() print(cleaned_text) # Output: " example"python
🌐
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 ...
🌐
ReqBin
reqbin.com › code › python › nxrhfweu › python-split-string-example
How do I split a string in Python?
An example of limiting the number of splits by passing the maxsplit parameter to the string.split() method. ... The string.rsplit() pair method is similar to the string.rsplit() method and has the same signature, but splits the string from right to left if the maxsplit parameter is specified. ... The Python splitlines() method splits a text at line breaks.
🌐
Dot Net Perls
dotnetperls.com › substring-python
Python - Substring Examples - Dot Net Perls
September 20, 2024 - When it reaches the requested number, it returns a slice of the string. Result When we use first_words, we extract the first words separated by spaces. A slice is used to take the substring. ... 0:i] return "" # Test our method. test = "Stately, plump Buck Mulligan came from the stairhead." result = first_words(test, ... In Python we extract parts of strings with slices.
🌐
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).
🌐
Software Testing Help
softwaretestinghelp.com › home › python programming for beginners – free python tutorials › python string functions – tutorial with examples
Python String Functions – Tutorial With Examples
April 1, 2025 - This Python tutorial will explain in detail about string in-built functions like String Reverse, Replace, Join, Split, Length, Compare & Lowercase and various other concepts like opening and deleting a file with the concerned examples in simple terms.