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
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.
Videos
String Alignment Methods in Python - YouTube
01:04
How to Right Justify a String in Python | Python String rjust() ...
03:39
Left Justify And Right Justify A String With ljust() rjust() | ...
12:22
Text data in python: How to Use Them | Python String Tutorial: ...
00:54
How To Highlight A Substring In A String In Python - YouTube
Python for Beginners: Introduction to String DataTypes
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.
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.
Reddit
reddit.com βΊ r/learnpython βΊ right align a string up to 20 characters and fill the rest of the space with *
r/learnpython on Reddit: Right align a string up to 20 characters and fill the rest of the space with *
January 27, 2025 -
This one is a bit hard to explain so I will just show the output for it. In this example the word is python, since python only has a length 6 characters, the rest of the space is filled with "*" to make the total number of characters 20 characters. To be honest I'm not even sure where start on this. Any help is appreciated.
Please type in a string: python **************python
Tutorialspoint
tutorialspoint.com βΊ python βΊ string_rfind.htm
Python String rfind() Method
The following example shows the usage of Python String rfind() method. Here, we create a string, say "This is a string", and pass a substring of this string, say "is", as an argument to the method.
Python documentation
docs.python.org βΊ 3 βΊ library βΊ re.html
re β Regular expression operations
May 25, 2026 - A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by '|' are tried from left to right.
Real Python
realpython.com βΊ python-strings
Strings and Character Data in Python β Real Python
December 22, 2024 - When you add a backslash before you press Enter, Python ignores the newline and interprets the whole construct as a single physical line. Sometimes, you need to include a literal backslash character in a string. If that backslash doesnβt precede a character with a special meaning, then you can insert it right ...
Dot Net Perls
dotnetperls.com βΊ right-python
Python - String Right Part - Dot Net Perls
December 13, 2021 - With a special method, we can get just the right part. But often using slice syntax directly is a better choice as it involves less code. Consider the string "soft orange cat." The last 3 characters are "cat," and so the result of right() with an argument of 3 should be "cat."