The Python way to map

printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)

would be

var1, var2 = raw_input("Enter two numbers here: ").split()

Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as delimiter as default. That means if we simply called split() then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,

Python has dynamic typing so there is no need to specify %d. However, if you ran the above then var1 and var2 would be both Strings. You can convert them to int using another line

var1, var2 = [int(var1), int(var2)]

Or you could use list comprehension

var1, var2 = [int(x) for x in [var1, var2]]

To sum it up, you could have done the whole thing with this one-liner:

# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]

# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
Answer from kizzx2 on Stack Overflow
Top answer
1 of 16
61

The Python way to map

printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)

would be

var1, var2 = raw_input("Enter two numbers here: ").split()

Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as delimiter as default. That means if we simply called split() then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,

Python has dynamic typing so there is no need to specify %d. However, if you ran the above then var1 and var2 would be both Strings. You can convert them to int using another line

var1, var2 = [int(var1), int(var2)]

Or you could use list comprehension

var1, var2 = [int(x) for x in [var1, var2]]

To sum it up, you could have done the whole thing with this one-liner:

# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]

# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
2 of 16
12

You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):

raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'

So you could rewrite your try to:

var1, var2 = raw_input("enter two numbers:").split(' ')

Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).

Also be aware that var1 and var2 will still be strings with this method when not cast to int.

🌐
Bobby Hadz
bobbyhadz.com › blog › python-split-string-into-multiple-variables
Split a String into multiple Variables in Python | bobbyhadz
April 8, 2024 - The str.split() method will split the string into a list of strings, which can be assigned to variables in a single declaration.
Discussions

Split string into different variables instead of array in Python - Stack Overflow
Possible Duplicate: Python Split String Is possible to directly split string into variables in one line, instead of using two lines. I'm sure that split would have two elements. Two lines examp... More on stackoverflow.com
🌐 stackoverflow.com
python - Split User input into different Variables - Stack Overflow
I'd like to take a user input (like an IP Address) and split the parts into separate variables for example 255.255.255.0 Now, I would like to split the string by the decimal points and save each... More on stackoverflow.com
🌐 stackoverflow.com
April 29, 2015
python - How to split a variable into two different variables? - Stack Overflow
A variable named "move" is used for the input. ... I want that variable to split into "action" and "object" variables by splitting the two words in half. ... Usually you post what you've tried first... But I'm Not A Wrapper Class – But I'm Not A Wrapper Class · 2014-01-13 00:57:19 +00:00 Commented Jan 13, 2014 at 0:57 ... My knowledge with Python ... More on stackoverflow.com
🌐 stackoverflow.com
Split a string into multiple int variables
I don't know about it being smart, but the string happens to be a literal 2-tuple of ints, so you could evaluate it as such: >>> from ast import literal_eval >>> x1, y1 = literal_eval('(100,200)') >>> x1 100 >>> y1 200 So for each operation (turn string into list, assign value to x1 y1, change value/type to int) I'd be using 5 rows, one for each Click variables. I couldn't understand what you're trying to say here, but if you're doing the same thing more than once, then you should be using a loop, not rewriting the same code multiple times. More on reddit.com
🌐 r/learnpython
9
1
January 19, 2022
🌐
Reddit
reddit.com › r/learnpython › splitting an input to give two types
r/learnpython on Reddit: Splitting an input to give two types
December 6, 2021 -

SOLVED: Can someone share a more pythonic method for splitting a space separated input, assigning the values to two different variables and changing one of the variable types to an integer and storing them in an OrderedDict, and collecting up the total number of items.

For example, you're given a number telling you how many sequences will follow, then a sequence strings, each with a fruit names (some with spaces) and the number of pieces of that fruit:

7
kiwi fruit 5
bananas 2
oranges 3
apples 2
jack fruit 3
kiwi fruit 2
apples 5

The method I've used so far uses OrderedDict and Counter to make an OrderedCounter (thanks Raymond Hettinger!), but I think my use of regular expressions here is a but janky. Any better/cleaner ways of doing this?

from collections import OrderedDict, Counter
import re

class OrderedCounter(Counter, OrderedDict):
    pass

if __name__ == '__main__':
    items_counts = OrderedCounter()
    for i in range(int(input())):
        name_count = re.split(r"\s(?=\d)", input())
        items_counts[name_count[0]] += int(name_count[1])
     
    
    for key in items_counts:
       print(f'{key} {items_counts[key]}')

_______________________________________________________________________________________________________

EDIT - new solution

Thanks to u/commandlineluser I've simplified this to:

from collections import OrderedDict

if __name__ == '__main__':
    items_prices = OrderedDict()
    for i in range(int(input())):
        name, price = input().rsplit(" ", 1)
        try:
            items_prices[name] += int(price)
        except:
             items_prices[name] = int(price) 
    for key, val in items_prices.items():
        print(f'{key} {val}')

🌐
GeeksforGeeks
geeksforgeeks.org › python › taking-multiple-inputs-from-user-in-python
Taking multiple inputs from user in Python - GeeksforGeeks
June 26, 2026 - One of the simplest ways to take multiple inputs from a user in Python is by using the input() function along with the split() method. The split() method splits a string into a list based on a specified separator (by default, it uses whitespace).
🌐
Scaler
scaler.com › home › topics › how to take multiple input in python?
How to Take Multiple Input in Python? - Scaler Topics
March 30, 2024 - To give multiple input in Python, the split() function in Python helps to split the multiple input fed by the user into single individual values for each variable. The method takes two parameters as part of its syntax that is, the separator and the maxsplit where the separator is any delimiter ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › split a string into multiple int variables
r/learnpython on Reddit: Split a string into multiple int variables
January 19, 2022 -

Howdy!

I have a table with some input parameters. A group of those parameters are (x,y) coordinates written in a single cell on excel as (x,y). The value is considered as a string "(x,y)", but I need to extract the values and put them into an Int variable.

#click1="[100,200]"
click1=click1.strip('][').split(',')
x1,y1=click1

The issue is that I have 5 Click variables (Click1, Click2, etc...), and the final variable x1, y1, etc... are type string and not int. So for each operation (turn string into list, assign value to x1 y1, change value/type to int) I'd be using 5 rows, one for each Click variables.

How can I do this in a smart way?

Cheers!

🌐
Medium
geekpython.medium.com › take-multiple-inputs-from-users-in-one-line-using-python-7c7594237f5a
Take Multiple Inputs From Users In One Line Using Python | by Sachin Pal | Medium
December 11, 2022 - In the first block of code, we specified the separator (,) and the inputs were separated by commas hence we got three separate outputs. In the second block of code, we specified a separator and maxsplit=1 and we can see the output is split into two parts instead of getting three separate outputs.
🌐
Plain English
plainenglish.io › home › blog › python › how to take multiple inputs from users in python
How to Take Multiple Inputs From Users In Python
July 11, 2021 - Generally, users use a split() method to split a Python string but one can use it for taking multiple inputs. ... separator: This is a delimiter. The string splits at this specified separator.
🌐
BitDegree
bitdegree.org › learn › python-split
Python split() Function: Learn to Split String Variables
October 3, 2019 - TL;DR – Python split() divides a string into a list of strings using a specified delimiter. ... String variables are useful tools for any Python programmer. They can contain numeric or alphanumeric information and are commonly used to store data directories or print messages. The .split() Python function is a commonly-used string manipulation tool. If you’ve already tried joining two strings in Python by concatenation, then split() does the exact opposite of that.
🌐
IncludeHelp
includehelp.com › python › taking-multiple-inputs-from-the-user-using-split-method.aspx
Taking multiple inputs from user in Python
# input two integer numbers and print them # Taking multiple inputs a, b = input("Enter two integer numbers : ").split(",") # Printing the values print("a :", a, "b :", b) # input name, age and percentages of the student # and print them name, age, perc = input("Enter student's details: ").split(",") print("Name :", name) print("Age :", age) print("Percentage :", perc) Enter two integer numbers : 100,200 a : 100 b : 200 Enter student's details: Bharat,21,99 Name : Bharat Age : 21 Percentage : 99 · Select the correct option to complete each statement about taking multiple inputs from the user in Python.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | split string into variables
Python | Split String into Variables - Be on the Right Side of Change
December 2, 2022 - Solution: As the number of variables is fixed, i.e., we have a fair idea about the number of variables required, you have to use the split() function on the given string which splits the given string into two items and stores them in a list.
🌐
DEV Community
dev.to › mohit355 › 3-5-split-method-in-python-2e19
3.5 split() method in Python - DEV Community
December 19, 2019 - grocery = 'Milk, Chicken, Bread, Butter' print(grocery.split(', ', 2)) print(grocery.split(', ', 1)) print(grocery.split(', ', 0)) print(grocery.split(', ', 5)) ['Milk', 'Chicken', 'Bread, Butter'] ['Milk', 'Chicken, Bread, Butter'] ['Milk, Chicken, Bread, Butter'] ['Milk', 'Chicken', 'Bread', 'Butter'] Developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.
🌐
W3Schools
w3schools.com › python › ref_string_split.asp
Python String split() Method
Remove List Duplicates Reverse ... Syllabus Python Study Plan Python Interview Q&A Python Training ... The split() method splits a string into a list....