Home
Aptitude
Categories


Home » Python » Python programs

Python | Check if a substring presents in a string using 'in' operator



An example of 'in' operator python: Here, we are going to learn how to check whether a substring presents in a string or not?
Submitted by IncludeHelp, on November 26, 2018

"in" operator is well known and most useful operator in python programming language, it can be used to check whether a substring is present in a string or not?

Example 1:

Input:
String: "IncludeHelp.com"
Substring to check: "Help"

Output:
Yes, substring presents in the string. 

Example 2:

String: "IncludeHelp.com"
Substring to check: "Hello"

Output:
No, substring does not present in the string.

Program:

# python program to check substring 
# presents in the string or not

# string and substring declaration, initialization
str = "IncludeHelp.Com"
sub_str ="Help"

# checking sub_str presents in str or not
if sub_str in str:
	print("Yes, substring presents in the string.")
else:
	print("No, substring does not present in the string.");

# testing another substring
sub_str = "Hello"	

# checking sub_str presents in str or not
if sub_str in str:
	print("Yes, substring presents in the string.")
else:
	print("No, substring does not present in the string.");

Output

Yes, substring presents in the string. 
No, substring does not present in the string.	





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.