Open In App

Python string | digits

Last Updated : 16 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

In Python3, string.digits is a pre-initialized string used as string constant. In Python, string.digits will give the lowercase letters ‘0123456789’.

Syntax : string.digits

Parameters : Doesn’t take any parameter, since it’s not a function.

Returns : Return all digit letters.

Note : Make sure to import string library function inorder to use string.digits

Code #1 :




# import string library function 
import string 
    
# Storing the value in variable result 
result = string.digits 
    
# Printing the value 
print(result) 


Output :

0123456789

 
Code #2 : Given code checks if the string input has only digit letters




# importing string library function 
import string 
     
# Function checks if input string 
# har only digits or not 
def check(value): 
    for letter in value: 
             
        # If anything other than digit 
        # letter is present, then return 
        # False, else return True 
        if letter not in string.digits: 
            return False
    return True
     
# Driver Code 
input1 = "0123 456 789"
print(input1, "--> ",  check(input1)) 
     
input2 = "12.0124"
print(input2, "--> ", check(input2)) 
     
input3 = "12345"
print(input3, "--> ", check(input3)) 


Output:

0123 456 789 -->  False
12.0124 -->  False
12345 -->  True

Applications :
The string constant digits can be used in many practical applications. Let’s see a code explaining how to use digits to generate strong random passwords of given size.




# Importing random to generate 
# random string sequence 
import random 
    
# Importing string library function 
import string 
    
def rand_pass(size): 
        
    # Takes random choices from 
    # ascii_letters and digits 
    generate_pass = ''.join([random.choice( string.ascii_uppercase + 
                                            string.ascii_lowercase + 
                                            string.digits) 
                                            for n in range(size)]) 
                            
    return generate_pass 
    
# Driver Code  
password = rand_pass(10
print(password) 
      


Output:

2R8gaoDKqn


Similar Reads

Sum of the digits of square of the given number which has only 1's as its digits
Given a number represented as string str consisting of the digit 1 only i.e. 1, 11, 111, .... The task is to find the sum of digits of the square of the given number. Examples: Input: str = 11 Output: 4 112 = 121 1 + 2 + 1 = 4 Input: str = 1111 Output: 16 Naive approach: Find the square of the given number and then find the sum of its digits. Below
6 min read
Python | Ways to remove numeric digits from given string
Given a string (may contain both characters and digits), write a Python program to remove the numeric digits from string. Let's discuss the different ways we can achieve this task. Method #1: Using join and isdigit() C/C++ Code # Python code to demonstrate # how to remove numeric digits from string # using join and isdigit # initialising string ini
6 min read
Python | Extract digits from given string
While programming, sometimes, we just require a certain type of data and need to discard other. This type of problem is quite common in Data Science domain, and since Data Science uses Python worldwide, its important to know how to extract specific elements. This article discusses certain ways in which only digit can be extracted. Let's discuss the
3 min read
Python | Split strings and digits from string list
Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let's discuss a way in which this task can be performed. Method #1 : Using list comprehension + strip() + isdigit() + join() The comb
5 min read
Python - Remove digits from Dictionary String Values List
Given list of dictionaries with string list values, remove all the numerics from all strings. Input: test_dict = {'Gfg' : ["G4G is Best 4", "4 ALL geeks"], 'best' : ["Gfg Heaven", "for 7 CS"]} Output: {'Gfg': ['GG is Best ', ' ALL geeks'], 'best': ['Gfg Heaven', 'for CS']} Explanation: All the numeric strings are removed. Input: test_dict = {'Gfg'
10 min read
Python - First K consecutive digits in String
Given a String and number K, extract first K consecutive digits making number. Input : test_str = "geeks5geeks43best", K = 2 Output : 43 Explanation : 43 is first 2 consecutive digits. Input : test_str = "geeks5gee2ks439best", K = 3 Output : 439 Explanation : 439 is first 3 consecutive digits. Method #1 : Using loop This is brute way in which this
5 min read
Python program to calculate the number of digits and letters in a string
Given a string, containing digits and letters, the task is to write a Python program to calculate the number of digits and letters in a string. Example:Input: string = "geeks2for3geeks" Output: total digits = 2 and total letters = 13 Input: string = "python1234" Output: total digits = 4 and total letters = 6 Input: string = "co2mpu1te10rs" Output:
7 min read
Python Program to Generate Random String With Uppercase And Digits
Generating a series of random strings can help create security codes. Besides, there are many other applications for using a random string generator, for instance, obtaining a series of numbers for a lottery game or slot machines. A random string generator generates an alphanumeric string consisting of random characters and digits. Let's see how we
3 min read
Python | Remove all digits from a list of strings
Given a list of strings, write a Python program to remove all digits from the list of string. Examples: Input : ['alice1', 'bob2', 'cara3'] Output : ['alice', 'bob', 'cara'] Input : ['4geeks', '3for', '4geeks'] Output : ['geeks', 'for', 'geeks'] Method #1: Python Regex Python regex pattern can also be used to find if each string contains a digit or
5 min read
Python | Convert list of tuples into digits
Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Let’s discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of list is by using re. C/C++ Code # Python code to co
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg