4

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]"
10
  • 1
    input() in Python 3, raw_input() in Python 2. Commented Jan 18, 2017 at 17:16
  • 5
    This has to be explained in most tutorials. Commented Jan 18, 2017 at 17:16
  • 4
    This is not a duplicate IMO, scanf is a very specific tool and I don't think raw_input will cover all of its functionalities Commented Jan 2, 2020 at 12:37
  • 1
    @Barmar input() or raw_input() is similar to gets but OP is asking for scanf. Commented May 3, 2022 at 11:10
  • 3
    @Barmar I think it is clear enough. Python have a string format operator '%d, %d' % (3, 4) which works just like what printf (or say, sprintf) do. So it could be easy to Python beginners with C background to ask "if there are something similar to scanf". Should OP include what scanf do (maybe copy what man says) in the post so it become clear? Commented May 3, 2022 at 17:25

4 Answers 4

7

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

input(). For example this could be used as:

Name = input("Please enter your name?")

For use in Python 2, this would be raw_input(). For example this could be used as:

Name = raw_input("Please enter your name?")

1 Comment

This does not explain how to get formatted input from the user. This would be closer to fgets() or (I hate to say it) gets().
-3

in Python 2, you can use input() or raw_input()

s = input()     // gets int value        

k = raw_input()   // gets string value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.