
- 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
Python – Test String in Character List and vice-versa
When it is required to test string in character list and vice-versa, a simple ‘in’ operator and ‘join’ method are used.
Below is a demonstration of the same −
Example
my_string = 'python' print("The string is :") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] print("The key is ") print(my_key) joined_list = ''.join(my_key) my_result = my_string in joined_list print("The result is :") if(my_result == True): print("The string is present in the character list") else: print("The string is not present in the character list")
Output
The string is : python The key is ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] The result is : The string is present in the character list
Explanation
A string is defined and displayed on the console.
The value for key is defined and displayed on the console.
The elements of the key are joined to make a string using .join() function.
This is assigned to a variable.
The string and the key are compared to see if string is present in the above mentioned list.
This result is assigned to a variable.
Based on the Boolean value in this result, the relevant message is displayed on the console.
- Related Articles
- Adding Tuple to List and vice versa in Python
- Convert string to DateTime and vice-versa in Python
- Converting string to number and vice-versa in C++
- Binary to decimal and vice-versa in Python
- Converting A Linked List Into An Array And Vice Versa
- How to convert String to StringBuilder and vice versa Java?
- Golang program to convert the arraylist into string and vice-versa
- How to Convert a String to Hexadecimal and vice versa format in java?
- Java Program to Convert the ArrayList into a string and vice versa
- How to Convert Vertical List to Horizontal or Vice Versa in Excel?
- Golang program to convert a linked list into an array and vice-versa
- Python – Test for Word construction from character list
- Find the difference of the sum of list elements that are missing from Matrix and vice versa in python
- Python – Character indices Mapping in String List
- Converting Strings to Numbers and Vice Versa in Java.

Advertisements