
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if a string is suffix of another in Python
Suppose we have two strings s and t. We have to check whether s is suffix of t or not.
So, if the input is like s = "ate" t = "unfortunate", then the output will be True.
To solve this, we will follow these steps −
- s_len := size of s
- t_len := size of t
- if s_len > t_len, then
- return False
- for i in range 0 to s_len, do
- if s[s_len - i - 1] is not same as t[t_len - i - 1], then
- return False
- if s[s_len - i - 1] is not same as t[t_len - i - 1], then
- return True
Let us see the following implementation to get better understanding −
Example
def solve(s, t): s_len = len(s) t_len = len(t) if (s_len > t_len): return False for i in range(s_len): if(s[s_len - i - 1] != t[t_len - i - 1]): return False return True s = "ate" t = "unfortunate" print(solve(s, t))
Input
"ate", "unfortunate"
Output
True
- Related Articles
- Check if suffix and prefix of a string are palindromes in Python
- How to check if string or a substring of string ends with suffix in Python?
- Python Check if suffix matches with any string in given list?
- How to check if a substring is contained in another string in Python
- How to check if a string ends with a specified Suffix string in Golang?
- How to check if a string is a subset of another string in R?
- Check if a string can be repeated to make another string in Python
- Check If a String Can Break Another String in C++
- Check if string contains another string in Swift
- Check if it is possible to transform one string to another in Python
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- Python - Check if a list is contained in another list
- Check if a string is Colindrome in Python
- Python - Check if a variable is string

Advertisements