[PYTHON] Standard input / output summary

I would like to summarize the standard input / output in various languages. We will add languages from time to time. Table of Contents

Caution

For C and C ++

#include <stdio.h> //c
#include <iostream> //c++

Is assumed to be written in the header. It works not only in C but also in C ++, which is often faster.

input

One value per line

Python3

s = input() #s:string
i = int(input()) #i:int
f = float(input()) #f:float

Python2

s = raw_input() #s:string
N = input() #Interpret the input as an expression

C

scanf("%c", &c); //c:char
scanf("%d", &i); //i:int(Decimal number)
scanf("%f", &f); //f:float
scanf("%s", s); //s:String array

C++

std::cin >> a;
* Addition He commented that the C and C ++ examples can cause unintended results. There was an article that you explained, so I will post it. Please see here for details. https://qiita.com/yumetodo/items/238751b879c09b56234b

List multiple values separated by spaces on one line

Python3

l = input().split() #Enter as a string l:list
l = list(map(int, input().split())) #Convert to integer l:list
l = [int(i) for i in input().split()] #Same as above Using list comprehension
#Which is faster, map or list comprehension, depends on the case

Python2

l = raw_input().split() #Enter as a string l:list
l = map(int, raw_input().split()) #Convert to integer l:list

C

for(int i=0,i<N,i++) scanf("%d", &l[i]); //Automatically recognize spaces as delimiters

C++

std::cin >> a; //slow

Multiple numbers as variables separated by spaces on one line

Python

a, b, c = map(int,input().split())

C

scanf("%d%d%d", &i, &j ,&k); //Automatically recognize spaces as delimiters

output

Python2

print a
print a, #Change line breaks to spaces
print(a) #Parentheses may or may not be present

Python3

print(a) #I need parentheses
print(a, b, c) #Multiple outputs(Space delimited)
print(a, b, c, sep=',') #Change the break
print(a, end=' ') #Change the last output character

C

printf("%s %s", "Hello", "world"); //'Hello, world'
printf("%c",'a'); //char
printf("%f", 3.14); //float
printf("%d", 334); //int(Decimal)
printf("%10s", "I'm fine."); //Character width(10)Designation
printf("%.2f", 3.14159); //"3,14"Number of digits after the decimal point(2)Designation These two can be set at the same time
printf("%.2e", 114.514); //"1.15e+02"Exponential notation
printf("%05d", 34); //"00034"0 packing
printf("%-10s", "Hello"); //Left
printf("%+d", 10); //"+10"Sign display

C++

std::cout << a << std::endl; //With line breaks Slow
std::cout << a << '\n'; //Fast with line breaks
std::cout << a; //No line breaks

Recommended Posts

Standard input / output summary
Standard input summary
Standard input / summary / python, ruby
[Python] Standard input
python input and output
Python audio input / output
Theme: Standard output [Difficulty ★★★★★]
Limited standard output problem
[Python] About standard input
Write standard input in code
Standard input with time limit
[Python3] Standard input [Cheat sheet]
Test standard output with Pytest
Python3 standard input (competition pro)
Receiving standard input tips @ python
[For AtCoder] Standard input memo
Decorator to silence standard output
[For beginners] Summary of standard input in Python (with explanation)
Python3 standard input for competitive programming
Matrix representation with Python standard input
Tips on Python file input / output
32-bit input / arbitrary bit output hash function
Input / output with Python (Python learning memo ⑤)
Write standard output to a file
Input / output method of values from standard input in competitive programming, etc.
Notes for Python file input / output
Python standard input summary that can be used in competition pro
Python Paiza-Various skill checks and standard input
Data input / output in Python (CSV, JSON)
Data handling 1 Data formatting and file input / output
Python3 standard input I tried to summarize
UnicodeEncodeError struggle with standard output of python3
Atcoder standard input set for beginners (python)
[Python] Add comments to standard input files
[Python] Chapter 02-03 Basics of Python programs (input / output)