Python Output All Suffixes of a String
In Python, we can use slicing operations to get all suffixes of a string. A string suffix is a substring that starts from a certain position in the string and goes to the end of the string. We can get all possible suffixes by iterating over each character position of the string.
Example
def get_all_suffixes(s):
suffixes = []
for i in range(len(s)):
suffixes.append(s[i:])
return suffixes
# Example string
s = "hello"
print(get_all_suffixes(s))
suffixes = []
for i in range(len(s)):
suffixes.append(s[i:])
return suffixes
# Example string
s = "hello"
print(get_all_suffixes(s))
Code explanation:
get_all_suffixes(s)is a function that takes a stringsas a parameter.suffixes = []Initialize an empty list to store all suffixes.for i in range(len(s)):Iterate over each character position of the string.suffixes.append(s[i:])Use slicing operations[i:]to get from positionithe substring from the start to the end of the string, and add it tosuffixesthe list.return suffixesReturn the list containing all suffixes.s = "hello"Define an example string.print(get_all_suffixes(s))Call the function and print the result.
Output result:
Example
['hello', 'ello', 'llo', 'lo', 'o']
Python3 Examples