Isdigit python 3 как использовать
Перейти к содержимому

Isdigit python 3 как использовать

  • автор:

Проверка типов#

При преобразовании типов данных могут возникнуть ошибки такого рода:

Ошибка абсолютно логичная. Мы пытаемся преобразовать в десятичный формат строку „a“.

И если тут пример выглядит, возможно, глупым, тем не менее, когда нужно, например, пройтись по списку строк и преобразовать в числа те из них, которые содержат числа, можно получить такую ошибку.

Чтобы избежать её, было бы хорошо иметь возможность проверить, с чем мы работаем.

isdigit #

В Python такие методы есть. Например, чтобы проверить, состоит ли строка из одних цифр, можно использовать метод isdigit :

isalpha #

Метод isalpha позволяет проверить, состоит ли строка из одних букв:

isalnum #

Метод isalnum позволяет проверить, состоит ли строка из букв или цифр:

Иногда, в зависимости от результата, библиотека или функция может выводить разные типы объектов. Например, если объект один, возвращается строка, если несколько, то возвращается кортеж.

Нам же надо построить ход программы по-разному, в зависимости от того, была ли возвращена строка или кортеж.

How to Check If a Python String Contains a Number — CODEFATHER

Claudio Sabato

Knowing how to check if a Python string contains a number can be something you will have to do at some point in your application.

A simple approach to check if a Python string contains a number is to verify every character in the string using the string isdigit() method. Once that’s done we get a list of booleans and if any of its elements is True that means

Claudio Sabato

Written by Claudio Sabato

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

How to Check if the String is Integer in Python

How to Check if the String is Integer in Python

The digital world of today is shifting toward data science and machine learning. «DATA» is the key point and the centroid for the upcoming technology. This data can take several forms, but it is typically in the form of values (numbers). There are numerous methods for gathering data but the most common way is using Strings.

In this article, you will learn how to check if a String is an integer in Python. Before moving on, let’s take a quick recap of Integers and Strings in Python.

What is an Integer?

An Integer is a whole number, which includes negative numbers, positive numbers, and zero. Integers don’t include any fractions or rational parts.

For example: 23, -39, 4 -are integers & 3.54, 6.34 are not integers.

What is a String in Python?

Strings are sequences of characters, enclosed between quotations, double-quotes (» «), or single-quotes (‘ ‘). These characters include alphabets (a-z) (A-Z), numbers (0-9), and special characters ( -, _ , %, $, & and more). In Python, any value enclosed between the quotation marks is termed as strings.

For example: «Favtutor» , ’54’, «ab1» , «sq_rt»

Why do we need to check for Integers in a String?

There are many instances where you would be required to check if a string is an integer or if a string contains any number. To be precise, you have to look out for cases where strings («45») are actually numbers (45).

Let’s take a few examples from our daily life, where you need to check for integers in a string:

  1. Validating user input
  2. Looking for data within strings
  3. Checking for valid input details in a field (Consider a form and a field for name)

How to Check If String is an Integer in Python?

Python offers many methods that check if a string has any number in it. Some of them are listed below:

Method 01) Using Error Handling with int()

This method is strictly to check for «integers» in Python. Using the exception-handling approach, we determine whether the «string» is an «integer» or not. This entails «trying» to typecast the string into an integer. If it does not throw any ValueErrors, it is an integer. On the contrary, if an error occurs, the except block is executed, which changes the flag to False.

Let’s see an example:

Note that I’ve initialized the flag to True, since we are changing it to False, in case of ValueError.

Output:

We may conclude from the output that the exception-handling approach only works for integers as strings and not for strings comprising both integers and strings (or other objects). Before we proceed, let’s apply this procedure to a variety of test cases:

This displays that this method works in identifying both, positive and negative integers, in a string. Though, when applied to an integer itself, it produces a False value. Do you know why?

Output:

Since we have tried and tested this method against all possible test cases, let’s list out the cases and whether this method works for them or not.

Input Type Input example Result obtained
Positive Integer as String «10» An integer
Negative Integer as String «-10» An integer
Decimal number as String «4.5» Not an Integer
A String «FavTutor» Not an Integer
An Integer and a String «10 Methods» Not an Integer
An Integer as Integer 10 Not an Integer

The above table displays the output of the exception-handling method in all possible test cases.

Method 02) Using isdigit()

The isdigit() method is an attribute of the string object to determine whether the string is a digit or not. This is the most known method to check if a string is an integer. This method doesn’t take any parameter, instead, it returns True if the string is a number (integer) and False if it’s not.

The code below displays the use of isdigit() method on all the potential test cases I could come up with. You are free to try the code on other test cases.

We have applied the isdigit() method to all the test cases as discussed above. You can observe the difference between the exception-handling method and isdigit() method from the output below:

Did you notice? The above code only worked for positive integers as a string. Unlike the exception-handling method, the isdigit() method doesn’t work for negative integers. Also, it gave an error when called on an integer (integer object). Why so? Remember that isdigit() is an attribute for the string objects and not integers!

In order to check for negative numbers using isdigit(), you’ll need to perform an additional check before checking for isdigit(). You can try:

Adding an additional check for symbols ( ‘+’ and ‘-«) ensures that when encountering a negative number, depicted by the ‘-‘ sign, the code doesn’t avoid the number and acknowledges it as an integer. Also, I’ve added another check in the else block. So that even when the string doesn’t have any sign as a prefix, it goes through isdigit() method.

Output:

As we did for the exception-handling method, let’s create a quick table for the cases where isdigit() method successfully works to check if a string is an integer in Python-

Input type Input example Result produced
Positive Integer as string «10» An integer
Negative Integer as string «-10» Not an integer. (Requires an additional check for signed numbers)
Float number as String «19.90» Not an Integer
String «FavTutor» Not an Integer
An Integer + String «10 methods» Not an Integer

Method 03) Using isnumeric() method

Python offers isnumeric() method that checks whether a string is an integer or not. This method is similar to the isdigit() method but with a few differences. The isnumeric() method checks whether all the characters in the string are numeric. While the isdigit() method checks whether the strings contain only digits. Also, isnumeric() method works for Unicode. It checks whether a character is a Unicode representation of a numeric value.

Now, you must be wondering about «Unicode»?

You’ll be surprised that Python strings use Unicode Standards to represent characters.

What is Unicode? Unicode is a specification with the objective of listing every character used by human languages. Well, there’s a long story behind the introduction of the Unicode Standard. But cutting through the chase, Unicode Standards were introduced so as to provide codes (computer-interpreted code like ASCII) for all the languages in the world. It gives each character its own unique code. Hence, making Python accepts formats in languages other than English, like Roman numerals.

Coming back to the isnumeric() method! Let’s take an example —

Output:

You can apply the same condition, as we did before, to check for the signs (‘+’ and ‘-‘). Hence, we can also detect negative numbers with isnumeric() method.

Method 04) Using re module

Python offers an in-built «re module» to work with Regular expressions. Actually, ‘re’ stands for «Regular Expression».

What are «Regular Expressions»? Regular Expressions, or RegEx, are a series of characters that form a search pattern. These expressions are used to find a certain pattern within a string.
Many functions in Python’s re-module can be used to determine whether a string is an integer. To be more specific, they «search» within the given string format for the defined pattern (in our case, digits). This specific pattern can be expressed by «metacharacters» or «special sequences.» Before moving on to the method, let’s learn about metacharacters and special sequences.

What are Metacharacters and Special Sequences? Metacharacters are characters with special meanings. They are commonly used in regular expressions to represent a set of characters or to mark the beginning, end, or occurrences of a string.

For example — » [] » represent a set of characters; » ^ » marks the beginning of a string and » \ » marks the beginning of a special sequence.

You must know about the set » [] » character in order to check if a string is an integer in Python using the re-module. The « [0-9] » returns a match for any digit between 0 and 9. (which we are looking for!)

A Special Sequence is also a group of characters with special meanings. The only difference is that it starts with » \ «. So, «\» followed by any character makes that sequence a special sequence.

For example — » \d » returns a match where the string contains digits (0-9); « \D » returns a match where the string does not contain digits and many more.

Let’s get back to using the RegEx functions to check if a string is an integer-

Function 1: Using re.search()

This method returns a Match object (an object containing information about the search and result) if there’s a match for the string. It searches for a match of the specified pattern (here 0-9) in the given string. Hence, it doesn’t strictly check if a string is only an integer. Rather it checks whether the string contains any digits.

Let’s take a look at an example before trying out the search() method with all our test cases:

It is important to import the re-module in order to call for the search() function. The re.search() function searches for digits [0-9] among the given string and returns a Match Object, as seen below-

The span attribute displays the start and end positions of the match. Note that the search() function stops as soon as it gets its first match. Also, it returns None in case no match is found.

Let’s try the search() function on all of our test cases:

Output:

Surprised by the Output? Instead of looking at whether the string is an integer, this method actually outputs whether a string contains any number, be it an integer or float or a mix of integers and strings.

Replace the [0-9] with «\d» and you will see your desired output-

Although this replacement also works only to distinguish the numbers from strings and not specifically for integers.

Output:

Function 2: Using re.findall()

The findall() function returns a list of all the digits it «finds» in the string. Unlike the search() function in the re module, it parses through the complete string and returns a list of all the digits (matching through the pattern [0-9]) in the string. Let’s take a quick example of how it works:

Output:

Note how the findall() function has listed out all the digits in the string. Let’s use the findall() function with all our test cases-

You can look for the testcases list from the above example. The findall() function returns an empty string in case it doesn’t find any match for the pattern in the string.

Output:

This function() also doesn’t work only for integers, but to list out (or check) if a string has any digit (0-9).

Function 3: Using re.match()

This function also returns a Match Object when it finds a match for the specific string ([0-9] or «[+-]?\d+$» ) in the given string. For instance,

Output:

Carefully observe the output. You’ll notice the difference between the search() and match() functions.

Hard to find? Let me help you!

You know that both, match() and search() functions, return Match Objects for the strings. The search() function returns as soon as it gets one of the digits while the match() function completes the «match» and returns the span (start-end index) for all the digits in the string.

I’ll leave the task of applying the match() function to you! Try out this function on different test cases and compare the outputs. You’ll have a better idea of how this module works.

If you are still confused about any of the above methods, we can connect you with a qualified python tutor online anytime to solve your queries.

Conclusion

Determining whether a string is an Integer in Python is a basic task carried out for user input validation. But there’s a difference between the task «Check if a String ‘is’ an Integer in Python» and «Check if a String ‘has’ any number in Python». In the above article, the isdigit(), isnumeric(), and exception-handling methods can be used specifically for the first, i.e. «To check if a string is an integer» whereas the re-module is used for the latter.

There are also methods like using any() and map() functions with isdigit() (or other specified methods). You can also find or even create different variations with the methods stated above. Each method has its own application area. Remember to compare different methods to find out the best one for your code. Happy Coding!

Isdigit python 3 как использовать

Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”.

Python String isdigit() Method Syntax

Parameters: isdigit() does not take any parameters

Returns:

  • True – If all characters in the string are digits.
  • False – If the string contains 1 or more non-digits.

Python String isdigit() Method Example

Python3

Output:

Example 1: Basic Usage

Python code for implementation of isdigit()

Python3

Output:

Example 2: Practical Implementation

Application: Using ASCII values of characters, count and print all the digits using isdigit() function.

Algorithm:

  1. Initialize a new string and a variable count=0.
  2. Traverse every character using ASCII value, check if the character is a digit.
  3. If it is a digit, increment the count by 1 and add it to the new string, else traverse to the next character.
  4. Print the value of the counter and the new string.

Python3

Output:

In Python, superscript and subscripts (usually written using Unicode) are also considered digit characters. Hence, if the string contains these characters along with decimal characters, isdigit() returns True. The Roman numerals, currency numerators, and fractions (usually written using Unicode) are considered numeric characters but not digits. The isdigit() returns False if the string contains these characters. To check whether a character is a numeric character or not, you can use isnumeric() method.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *