This question already has answers here:
How to concatenate two integers in Python?
(17 answers)
Closed 8 years ago.
How would I append a number to the end of another number?
for example:
a = 1
b = 2
a = a + b
print(a)
Output = 3
I want to get my output as 12 but instead I get 3. I understand that when you do number + number you get the addition of that but I would like to do is append b to a and so I would get 12. I have tried appending a number to a number but I get an error. I think that you can only append a list.
My question is:
How do I append a number to a number?
or is there a better way of doing it?
The reason you get 3 is because a and b contain integers. What you want is string concatenation to get 12. In order to use string concatenation you need strings. You can type cast the integers to string using str() and then use int() to type cast the string to an integer.
a = 1
b = 2
a = str(a) + str(b)
a = int(a)
print(a)
The oneliner solution is already provided in the comments.
Related
This question already has answers here:
Python: Strip off string quotes from binary number
(5 answers)
Closed 1 year ago.
b = 15
a = bin(b) # I want return as an integer not string
print(a, type(a)) # output is string, actually I want is integer
# output - 0b1111 <class 'str'>
So, I want to get bin() function return as an integer
The int function is used to convert to the integer. We need to pass the number and its base to convert it into an integer (since, the base for binary values is 2).
a = int('101',2)
print(a)
If you question is about converting for example 5 into bin in python, the bin function actually gives 0b101 as the result. So the simple trick to get 101 as an int is 👇
intnum=int(bin(number)[2:])
This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want
This question already has answers here:
Replace first occurrence of string in Python
(2 answers)
Closed 4 years ago.
Currently, i have a bit string represented as
current = "011"
and what I'm trying to do is to create a new string based of the bit string above with the 1 at index 1 replaced with 011 which would give me:
new = "00111"
The problem I'm having is that when I use the replace function, it replaced all the 1 in the string including the one at index 2 which is not what I desired.
new = current.replace("1","011")
= 0011011 #not what I wanted
Would appreciate some help on this.
Limit the number of replace to 1 such as below:
new = current.replace("1","011", 1)
This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
How to split a string of space separated numbers into integers?
(9 answers)
Closed 4 years ago.
I am trying to take in a string of numbers, such as:
1 2 3 4 55 33 15
and store them in a list as they appear so that I can operate on the items at each index in the list, but they aren't splitting and storing right. They store like:
1,2,3,4,5,5,3,3,1,5
which is not what I want. My current code is attached below. I thought they would store as:
1,2,3,4,5,55,33,15
I do convert them to ints later on and that works fine, this is just the part that I am stuck on.
numbersArrayTemp.append(userInput.readline().strip(' ').strip('\n'))
for item in numbersArrayTemp:
print(item)
Type = item.strip('\n')
print(Type)
j = item.replace(" ", '')
numbersArrayString.extend(j)
This question already has answers here:
How to print list item + integer/string using logging in Python
(2 answers)
Closed 7 years ago.
I am coding in Python, and have reached an error that I cannot seem to solve. Here's the part of the code that it affects.
import random
a = raw_input("Enter text")
b = random.randrange(1,101)
print (a+b)
When I try to run the code, I get the error "TypeError: cannot concatenate 'str' and 'int' objects"
I want to know how to print the result of a+b.
To answer to the question in the title, you can convert an integer into a string with str. But the print function already applies str to its argument, in order to be able to print it.
Here, your problem comes from the fact that a is a string while b is an integer. The + operator works on two strings, or two ints, but not a combination of this two types. If you have two strings, the + will mean concatenate. If you have two ints, the + will mean add. It then depends on the result you want to get.
You can convert a string to an integer by using int.
Try this code:
import random
a = int (raw_input ("Enter int "))
b = random.randrange (1, 101)
print a + b