Check Letter in a String Python
Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three types they are:
i)Using 'in' operator
ii)Using str.find() function
iii)Using Regex
Let us now discuss each method in detail, starting with 'in' operator in Python to check for a letter in the string
1. Using in Operator
We will discuss checking the letter in two ways, one by pre-defining a variable with a certain string and the other by taking a user input using the pre-defined input function available in Python by passing a prompt into the braces.
Code
variable_1 = 'javatpoint'
if 't' in variable_1:
print('letter exists')
else:
print('letter doesnot exist')
Output
letter exists
In the above code, we see that we have declared the variable as variable_1 with a string value as 'javatpoint' and by using the if and else condition, we are checking if the letter 't' exists in the string or not. Since the letter t exists in the variable we passed, it returned the if condition's print statement saying the letter exists.
Code
variable_1 = input('enter the word to check the letter : ')
if 'engi' in variable_1:
print('letter exists')
else:
print('letter doesnot exist')
Output
enter the word to check the letter : software engineer
letter exists
Now in the above code, we are checking for not a single letter but a group of characters to check the functionality of the in operator. We see that using the input function in the variable_1 and prompt saying enter the word to review the letter, the input from the user will be taken as soon as the code runs. By using the if and else condition, we are checking for the group of characters 'engi', which are a part of the word software engineer. When the code is set to run compiler asks for input; assume in this case we are giving input as a software engineer as the group of characters are present in the input variable, the condition gets triggered and executes the block of code below in the print statement saying that the letter exists.
2.Using str.find() Function
Python is known for its easiness, being handy and effectively managing code. We have many built-in functions that make the programming language stand out amongst others, so in our case, the str.find() function makes our job easy here. The short form of the function stands for string find. Not necessary that we have to use str as the prefix. We can use anything like 's' or 'string'. The catch here is to use the dot find function ideally.
Code
variable_1 = 'javatpoint'
letter = 'poi'
if variable_1.find(letter) !=-1:
print('letter exists')
else:
print('letter doesnot exist')
Output
letter exists
In the above block of code, we see that we have declared the variable_1 with the value javatpoint and another variable with the value of a group of characters 'poi' now, as the letter variable exists in the variable_1, the if condition gets executed and prints that letter exists.
3. Using Regex
The third primary approach to finding a letter in a string is using the Regex. Regex stands for regular expression. When we import the 're' module, we can start using the regular expressions at the beginning of the code.
Code
import re
text = 'match is cancelled due to rain'
x = re.search('^match.*rain',text)
if x:
print('yes the text starts with match and ends with rain')
else:
print('no it doesnot start with match and doesnot ends with rain')
Output
yes the text starts with match and ends with rain
As we see in the above code, instead of checking for a letter or a group of letters, we check if the text starts with a particular word and ends with a specific term.