×

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.


Related Topics

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.