Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function for taking formatted input from user similar to scanf() in 'C'

Tags:

python

I was wondering if there is a function in Python to take formatted input from user similar to taking the input from user in 'C' using scanf() and format strings such as %d, %lf, etc.

Hypothetical example in which scanf() returns a list:

input_date_list = scanf("%d-%d-%d")
# User enters "1969-04-20"
input_time_list = scanf("%d:%d")
# User enters "04:20"
print(input_date_list, input_time_list)
# Output is "[1969, 4, 20] [4, 20]"
like image 205
Andy.inamdar Avatar asked Oct 31 '25 03:10

Andy.inamdar


2 Answers

What the Standard Library provides

There is no direct scanf(3) equivalent in the standard library. The documentation for the re module suggests itself as a replacement, providing this explanation:

Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf() format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.

Modifying your hypothetical example, to work with regular expressions, we get...

import re

input_date_list = re.match(r"(?P<month>[-+]?\d+)-(?P<day>[-+]?\d+)-(?P<year>[-+]?\d+)", input())
print(f"[{input_date_list['year']}, {input_date_list['month']}, {input_date_list['day']}]")

input_time_list = re.match(r"(?P<hour>[-+]?\d+):(?P<minute>[-+]?\d+)", input())
print(f"[{input_time_list['hour']}, {input_time_list['minute']}]")

...and when executed:

python script.py << EOF
1-2-2023
11:59
EOF
[2023, 1, 2]
[11, 59]

Community Implementation

The scanf module on Pypi provides an interface much more akin to scanf(3).

This provides a direct implementation of your hypothetical examples:

from scanf import scanf

input_date_list = scanf("%d-%d-%d")
input_time_list = scanf("%d:%d")

print(input_date_list, input_time_list)

...and when executed:

python script.py << EOF
1-2-2023
11:59
EOF
(1, 2, 2023) (11, 59)
like image 171
tonymke Avatar answered Nov 02 '25 16:11

tonymke


There is no (built-in) direct and easy way to specify the input's format in Python.

The input function in Python 3 (raw_input in Python 2) will simply return the string that was typed to STDIN. Any parsing will be done manually on the string.

The input function in Python 2 (eval(input()) in Python 3, which is not recommended) did a very basic built-in parsing and would work for only a single element (i.e. equivalent to scanf("%d") for instance).

With some basic parsing you can get to not-so-complicated code that emulates scanf:

# scanf("%d-%d-%d")
input_date_list = [int(x) for x in input().split('-')]

# scanf("%d:%d")
input_time_list = [int(x) for x in input().split(':')]

For anything more complicated, more lines of code are needed. For example:

# scanf("%d,%f - %s")
nums, s = input().split(' - ')
i, f = nums.split(',')
i = int(i)
f = float(f)
like image 20
Tomerikoo Avatar answered Nov 02 '25 17:11

Tomerikoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!