c has scanf, does python have something similar? [duplicate] - python

This question already has answers here:
sscanf in Python
(9 answers)
How do I read from stdin?
(25 answers)
Closed 16 days ago.
I am trying to solve problems from SPOJ. I need to be able to read input from stdin for that, I did a lot of problems in C using scanf but wanted to try Python as well. How do i read the stdin inputs in Python? (wanna use Python 2.6/2.7)

In Python 2.7
To get integers or floats as inputs you can use the key word 'input'
Example: temp=input("Give your value")
Here temp only takes a float or int
There is another command raw_input() any value that raw input is given it converts it to string and assigns the value
Example:temp=raw_input("Give your value")
Here temp is of string type

For what I researched, in python 2.7(in all versions of python 2) the command of input is: raw_input( )
in this link have a tutorial about this:
https://linuxconfig.org/how-to-obtain-an-user-input-with-python-raw-input-function-example

Related

convert a string written in python to execute inside a python. may also be a complete scirpt as string with user inputs in between [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a string 'user = input("Hello World")'. How can I execute the string to be able to run as a python command?
I am a new to python so I must have missed the basic syntax. please help me instead of routing me to another answer
You can use either of two exec or eval to run the string as python command based on your desired syntax or result
eval('user = input("Hello World")')
exec('user = input("Hello World")')
Visit exec and eval to learn more about them

Saving a file name as a input variable [duplicate]

This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Putting a variable into a string (quote)
(4 answers)
Closed 2 years ago.
very new to python so sorry for the silly question
I've created a user input interface
fn=input()
#Where the user will input version32 for instance so effectively
fn=Version32
I've then imported a template word document using docx, which has been heavily modified based upon user input. I then want the file name output to be saved as "fn" or in this case Version32
output.save(r"C:\Users\XXX\XXX\XXX\'fn'.docx")
Where fn is a variable? Is this even possible, or am I barking up the wrong tree?
Kind Regards!!
IIUC, you can do this using f-string:
output.save(rf"C:\Users\XXX\XXX\XXX\{fn}.docx")

re.sub and re.subn [duplicate]

This question already has answers here:
How to check if re.sub() has successfully replaced in python? [duplicate]
(2 answers)
Python: how to substitute and know whether it matched
(3 answers)
Closed 2 years ago.
I want to use re.subn in my program to get no of times changes are made but I can't figure it out how to write the code. I have written the program with re.sub and it works fine but If I use (re.sub(r'\bi\b','I',contents)) in this program it shows this error.
TypeError: write() argument must be str, not int
import re
with open ('Sample.txt','r') as rf:
contents=rf.read()
change=(re.sub(r'\bi\b','I',contents))
with open ('Sample2.txt','w') as wf:
wf.writelines(change)
Can someone please guide how can I use re.subn to get the count.
Thank you.

Execute instructions written in a string [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a little problem, I have written a for loop as a string.
In PHP, with the help of function exec(), we can run the string which will eventually run the for loop defined inside the string.
Can we do such a thing in Python as well?
By example, I would like run follow it:
string="for i in range(1,(5+1)): print(str(i))"
How to run this in Python?
You can use exec if you want to execute some statements:
code = 'for i in range(1,(5+1)): print(str(i))'
exec(code)
If you want to evaluate an expression and get the value then you can use eval:
value = eval('2+3')
print(value) # 5

Input Statement Only Returning Str Type - Jupyter Notebook [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
Having trouble understanding why this is returning a str although I entered an int. Could someone please explain this? When you enter a value in input does it only capture the value as a string?
In Python 3.x, input returns the entered value as is (str) instead of evaluating it, so you should do int(input('Please input number:')) to get the value as an int.
In Python 2.x, however, raw_input would return the raw str value, while input would evaluate the entered value, as you can read on the docs:
input([prompt])
Equivalent to eval(raw_input(prompt)).
...
Consider using the raw_input() function for general input from users.
You can mimic this behavior in Python 3.x with eval: eval(input('Please input number:')) if you really need to, but first take a look at Security of Python's eval() on untrusted strings?

Categories

Resources