C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: We do not specify an end—this means the slice continues to the end of the string.
And: We use a negative start—this means we start from that many characters from the end of the string.
Python program that gets right part of strings
def right(value, count):
# To get right part of string, use negative first index in slice.
return value[-count:]
# Test the method.
source = "soft orange cat"
print(right(source, 3))
print(right(source, 1))
# We can avoid the "right" method entirely.
# ... We can also use len in the expression.
print(source[-2:])
print(source[len(source) - 2:])
Output
cat
t
at
at