- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether given string can be generated after concatenating given strings in Python
Suppose we have two strings s and t and r, we have to check whether r = s | t or r = t + s where | denotes concatenation.
So, if the input is like s = "world" t = "hello" r = "helloworld", then the output will be True as "helloworld" (r) = "hello" (t) | "world" (s).
To solve this, we will follow these steps −
- if size of r is not same as the sum of the lengths of s and t, then
- return False
- if r starts with s, then
- if r ends with t, then
- return True
- if r ends with t, then
- if r starts with t, then
- if r ends with s, then
- return True
- if r ends with s, then
- return False
Let us see the following implementation to get better understanding −
Example Code
def solve(s, t, r): if len(r) != len(s) + len(t): return False if r.startswith(s): if r.endswith(t): return True if r.startswith(t): if r.endswith(s): return True return False s = "world" t = "hello" r = "helloworld" print(solve(s, t, r))
Input
"world", "hello", "helloworld"
Output
True
- Related Articles
- Check if given string can be formed by concatenating string elements of list in Python
- Check if given string can be split into four distinct strings in Python
- Print the given 3 string after modifying and concatenating
- Generating random strings until a given string is generated using Python
- Check if a string can be formed from another string using given constraints in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Check whether the given string is a valid identifier in Python
- Check if a two-character string can be made using given words in Python
- Check whether two strings are equivalent or not according to given condition in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Check whether second string can be formed from characters of first string in Python
- Check whether the number can be made perfect square after adding 1 in Python
- Python program to check whether a given string is Heterogram or not
- How to check whether the given strings are isomorphic using C#?
- Check if characters of a given string can be rearranged to form a palindrome in Python

Advertisements