A very simple question, if write
I want to write two things, how should I do it?
An example:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
entrada = input("Introduce el angulo: ")
ser.write(str(entrada))
What do I have to do here so that he write
writes la entrada
and then a 1 after it and that, if I entrada
write 90 , he write
sends 901 ?
You just have to concatenate both strings:
Or using
str.format()
:In Python >= 3.6 you can use formatted string literals:
It is important to ensure that
write
bytes are passed to it and not an encoded string or other data type (hence the use ofencode
). In Python 2, if we are sure that the result is of type,str
it is not necessary. This code is valid for Python 2 and Python 3.On the other hand, using
input
Python 2 for unfiltered user input is very dangerous since it executes all valid code entered. Instead you should useraw_input
which, asinput
in Python 3, returns an objectstr
. See Doubt with raw_input() for more information.