How to get ACSII value of an integer in python? [duplicate] - python

This question already has answers here:
Convert int to ASCII and back in Python
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I have something like this right now but this wont work because chr requires an integer. How can I make it print the chr of the user input?
num1 = input("Enter a ASCII code 0 - 127:")
print(str(chr(num1)))

Just cast the user input to int first -
num1 = input("Enter a ASCII code 0 - 127:")
print chr(int(num1))
(Incorporated #chepner's comment)

Related

Looping to get a binary return statement [duplicate]

This question already has answers here:
Python int to binary string?
(36 answers)
Closed last year.
How can i loop through a decimal number and print out a binary figure for that decimal number.
This is the code i tried using python
dec_Num = 1200
for i in dec_Num;
print i
Because the goal is to print we can generate a string
def DecimalToBinary(num):
strin=""
while num >= 1:
strin+=str(num%2)
num=num // 2
return strin[::-1]

What is wrong in this Python Sum Program? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
a=input("Enter First Number ")
b=input("Enter second number ")
c=a+b
print("The Sum of two numbers are ",c)
"Why This Program not printing Sum of tho number?s?""
You need to cast the numbers to int. When you take an input, it is always a string. To cast a string to int, you can do int(a)+int(b)

Take integer from input with string and integer [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I want to figure out how to take the integer from a input when there is both a integer and string.
For example, if I input "hello 3", is there a way I can separate the "3" to another variable aside from the "hello"?
Would this work for you:
myInput=input() # Get Input
myString,myIntStr=myInput.split(" ") # Split in to list based on where the spaces are
myInt=int(myIntStr) # Convert to int

How do you add with variables in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I tried this:
a = input()
print("+")
b = input()
c =a+b
print(c)
But it output 55 instead of 10 when I input 5 + 5
You are concatenating strings instead of adding integers, you can use int() to convert from string to integer and str() to convert from integer to string.
If you run print(5+5) you will get 10.
If you run print('5'+'5') you will get 55 because the + operator will concatenate strings together.

How to convert str to hex via python [duplicate]

This question already has answers here:
Print a string as hexadecimal bytes
(13 answers)
Closed 7 years ago.
I want to convert string to a hex (decimal),
for example abc to 616263 and so on.
How do I do that?
I have tried:
x= input("Enter string")
x = int(x)
hex(x)
But it is not working.
maybe you are looking for something like:
x.encode("hex")

Categories