This question already has answers here:
Keeping only certain characters in a string using Python?
(3 answers)
Closed 7 months ago.
Can someone please hint my mistake. Im trying to remove numbers from the string. Findall succefully finds the number but replace does not replace them with empty string. here is the code:
import re
x = 'John2345 can5 code with python3'
extra = re.findall('\d{1,9}', x)
x.replace(extra, '')
x
and here is the error:
TypeError Traceback (most recent call last)
<ipython-input-334-bfc161d88f65> in <module>()
2 x = 'ererf ergg5 erg 545 eeg'
3 extra = re.findall('\d{1,9}', x)
----> 4 x.replace(extra, '')
5 x
TypeError: Can't convert 'list' object to str implicitly
Cheers,
Sia
Use re.sub()
re.sub(r'\d+' ,'', x)
x is a string, extra is a list, right? The best hint is in the error message you recieved:
TypeError: Can't convert 'list' object to str implicitly
What python actually tells you, in human language is "I can't get your list object as argument here!! give me a string!!!"
Related
This question already has answers here:
How to resolve TypeError: can only concatenate str (not "int") to str [duplicate]
(6 answers)
Closed 2 years ago.
This works,
In [2]:
"I said " + ("Hey " * 2) + "Hey!"
Out[2]:
'I said Hey Hey Hey!'
but this doesn't. Why?
In [3]:
"The correct answer to this multiple choice exercise is answer number " + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
"The correct answer to this multiple choice exercise is answer number " + 2
TypeError: must be str, not int
It's okay to multiply a string by an integer -- the result is N repetitions of the string.
I's not okay to add a string to an integer. The people who designed Python decided that this is not allowed.
That's just how Python works.
This question already has an answer here:
How can I concatenate str and int objects?
(1 answer)
Closed 2 years ago.
I am currently learning Python so I have no idea what is going on.
import random
x=10
while x>0:
print(x+'='+(random.randint(1,100)))
x-=1
When I run the program
Traceback (most recent call last):
File "C:\Users\black\.spyder-py3\temp.py", line 12, in <module>
print(x+'='+(random.randint(1,100)))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Your x is integer, but you are trying to concatenate it with string (equal sign).
You need to convert both x and the random value after equal sign to string:
import random
x=10
while x>0:
print(str(x)+'='+str(random.randint(1,100)))
x-=1
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
My goal is very simple, which makes it all the more irritating that I'm repeatedly failing:
I wish to turn an input integer into a string made up of all numbers within the input range, so if the input is 3, the code would be:
print(*range(1, 3+1), sep="")
which obviously works, however when using an n = input() , no matter where I put the str(), I get the same error:
"Can't convert 'int' object to str implicitly"
I feel sorry to waste your collective time on such an annoyingly trivial task..
My code:
n= input()
print(*range(1, n+1), sep="")
I've also tried list comprehensions (my ultimate goal is to have this all on one line):
[print(*range(1,n+1),sep="") | n = input() ]
I know this is not proper syntax, how on earth am I supposed to word this properly?
This didn't help, ditto this, ditto this, I give up --> ask S.O.
I see no reason why you would use str here, you should use int; the value returned from input is of type str and you need to transform it.
A one-liner could look like this:
print(*range(1, int(input()) + 1), sep=' ')
Where input is wrapped in int to transform the str returned to an int and supply it as an argument to range.
As an addendum, your error here is caused by n + 1 in your range call where n is still an str; Python won't implicitly transform the value held by n to an int and perform the operation; it'll complain:
n = '1'
n + 1
TypeErrorTraceback (most recent call last)
<ipython-input-117-a5b1a168a772> in <module>()
----> 1 n + 1
TypeError: Can't convert 'int' object to str implicitly
That's why you need to be explicit and wrap it in int(). Additionally, take note that the one liner will fail with input that can't be transformed to an int, you need to wrap it in a try-except statement to handle that if needed.
In your code, you should just be able to do:
n = int(input())
print(*range(1,n+1),sep="")
But you would also want to have some error checking to ensure that a number is actually entered into the prompt.
A one-liner that works:
print(*range(1, int(input()) + 1), sep="")
This question already has answers here:
TypeError: 'int' object is not callable
(10 answers)
Closed 7 years ago.
I'm having trouble using sum for summing up list items. It always gives me the following error: TypeError: "'int' object is not callable". The questions was asked before, and the best response was the code below:
a = range(10)
# [0,1,2,3,4,5,6,7,8,9]
b = sum(a)
print(b)
# prints 45
It also gives me the same error. Can anyone help me out? I'm using Spyder (python 3.5)
Thanks in advance!
You bound the name sum to an integer:
>>> sum = 42
>>> sum(range(10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Python doesn't prevent you from using the same name for a built-in function; it is your responsibility to not use those names if you need to use the built-in. Rename the sum variable in your code, or if this is in the interpreter, simply delete the variable:
>>> sum
42
>>> del sum
>>> sum(range(10))
45
This question already has answers here:
Why does substring slicing with index out of range work?
(3 answers)
Why doesn't Python throw an error for slicing out of bounds? [duplicate]
(4 answers)
Closed 9 years ago.
I have just started learning python and I was playing with the slice (:) operator and strings. For some reason if I specify an invalid index followed by the slice operator it does not give the "Out of range" error.
>>> s='Hello'
>>> print s[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> print s[5:]
//Prints just a newline, no error
Can someone please explain why I am not getting error in the 2nd case.
From the documentation on slicing:
Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.
In other words: the index can be too big but in that case it's replaced by the string size.
Note that your string s has five characters so s[5:] is the same as calling s[len(s):] which is the same as getting an empty string (as the missing upper bound defaults to len(s) as well, so it ultimately becomes s[len(s):len(s)]).