2

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]"
Felix An
  • 309
  • 4
  • 13
Andy.inamdar
  • 303
  • 1
  • 3
  • 10
  • 1
    `input()` in Python 3, `raw_input()` in Python 2. – Barmar Jan 18 '17 at 17:16
  • 5
    This has to be explained in most tutorials. – Barmar Jan 18 '17 at 17:16
  • a tutorial: http://python3porting.com/differences.html#input-and-raw-input – chickity china chinese chicken Jan 18 '17 at 17:21
  • dont ask me the que dude... – Andy.inamdar Jan 18 '17 at 17:38
  • OP should probably delete this question, which will regain rep. – ti7 Jan 25 '18 at 20:19
  • 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 – radrow Jan 02 '20 at 12:37
  • I agree with @radrow, in python you can't read in a formatted style, like `scanf("%d %f", &my_int, &my_float)`. In python after reading with input() you should parse every entry in the input to typecast and assign variables. – Jader Martins Apr 21 '22 at 23:27
  • 1
    @Barmar `input()` or `raw_input()` is similar to `gets` but OP is asking for `scanf`. – tsh May 03 '22 at 11:10
  • @tsh The question is so vague it's hard to tell what they're really asking for. – Barmar May 03 '22 at 13:54
  • 2
    @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](https://linux.die.net/man/3/scanf)) in the post so it become clear? – tsh May 03 '22 at 17:25

4 Answers4

3

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)
tonymke
  • 752
  • 1
  • 5
  • 16
1

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?")
Blade
  • 984
  • 3
  • 12
  • 34
CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
  • 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()`. – Felix An Nov 25 '22 at 09:07
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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
-2

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

s = input()     // gets int value        

k = raw_input()   // gets string value
OzizLK
  • 47
  • 2
  • 11