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 Top answer 1 of 8
140
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]
2 of 8
38
If I remember my QBasic, right, left and mid do something like this:
>>> s = '123456789'
>>> s[-2:]
'89'
>>> s[:2]
'12'
>>> s[4:6]
'56'
http://www.angelfire.com/scifi/nightcode/prglang/qbasic/function/strings/left_right.html
Videos
Python for Absolute Beginners Course - String Slicing
00:59
How To Slice A String In Python - YouTube
06:37
String Manipulation (Left, Mid & Right) in Python - YouTube
06:47
๐ Python Tutorial #26: String Slicing - YouTube
09:59
Python 60 String Left Strip - YouTube
06:31
String Manipulation - Python Tutorial + Full Explanation - YouTube
YouTube
youtube.com โบ learn easy steps
How to use Left function in Python | How to extract characters from Left in Python | Learn Python - YouTube
How to use Left function in Python. Explained in step by step approach with an example on how to use str[:] operation in Python. Extracting text from left in...
Published ย August 6, 2021 Views ย 317
Apache
spark.apache.org โบ docs โบ latest โบ api โบ python โบ reference โบ pyspark.sql โบ api โบ pyspark.sql.functions.left.html
pyspark.sql.functions.left โ PySpark 4.1.2 documentation
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.
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.
InterviewQs
interviewqs.com โบ ddi-code-snippets โบ substring-python
Slice a string in python (right, left, mid equivalents) - InterviewQs
'8754321' #right 2 characters string[-2:] '21' #left 2 characters string[:2] '87' #4th through 6th characters (index starts at 0) string[4:6] '32'
Top answer 1 of 2
9
You're looking for slicing:
>>> s = "Hello World!"
>>> print s[2:] # From the second (third) letter, print the whole string
llo World!
>>> print s[2:5] # Print from the second (third) letter to the fifth string
llo
>>> print s[-2:] # Print from right to left
d!
>>> print s[::2] # Print every second letter
HloWrd
So for your example:
>>> s = 'col555'
>>> print s[3:]
555
2 of 2
2
If you know it will always be col followed by some numbers:
>>> int('col1234'[3:])
1234
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.
Inductive Automation
forum.inductiveautomation.com โบ general discussion
String Manipulation - General Discussion - Inductive Automation Forum
February 1, 2016 - I am trying to manipulate a string from an OPC tag using the Left function. I am able to return the full string without issue. After several failed attempts, I copied the text from the Hello World example given for the left function as a test and it also returned an error stating โNameError: ...
GeeksforGeeks
geeksforgeeks.org โบ python โบ ways-to-apply-left-right-mid-in-pandas
Ways to apply LEFT, RIGHT, MID in Pandas - GeeksforGeeks
July 28, 2020 - # importing pandas library import pandas as pd # creating and initializing a list Cars = ['1000-BMW','2000-Audi','3000-Volkswagen', '4000-Datsun','5000-Toyota','6000-Maruti Suzuki'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters from right side # using slicing and storing result in # 'Left' Left = df['Model_name'].str[:4] print(Left)
DigitalOcean
digitalocean.com โบ community โบ tutorials โบ python-string-functions
Python String Functions: Complete Guide with Examples | DigitalOcean
August 3, 2022 - Master Python string functions like split(), join(), replace(), strip() and more. Comprehensive guide with practical examples for string manipulation.