python 3 int() problems - python

nextt[0:1] = "*2"
rds = int(nextt[0:1].replace("*",""))
And there is problem, it says:ValueError: invalid literal for int() with base 10: ''
I just need to delete "*" from string and convert it to int.

You are slicing just one character:
>>> '*2foo'[0:1]
'*'
Replacing the * gives you an empty string. Perhaps you wanted to slice two characters?
>>> '*2foo'[:2]
'*2'
If you are slicing anyway, just pick the digit character without the *:
int(nextt[1])

int('*2'.replace('*', ''))
'*2'.replace('*', '') this replaces * with empty string and results in '2'
Now you cast it to int.

Related

How to convert a string to a number where number is embed in further string

hi I have a string like this
a = "'0123','0124'"
and I am trying to convert it into something like this b = (0123,0124)
and doing something like this
b = int(a.replace(',', ''))
and getting error
ValueError: invalid literal for int() with base 10:
Try:
b = tuple(int(i[1:-1]) for i in a.split(','))
print(b)
# Output
(123, 124)
Remove the quotes, split the string at comma, and use int() to convert each of them to an integer.
b = [int(x) for x in a.replace("'", "").split(',')]
Leading zeroes don't make any sense in this result. They're part of a printed representation of a number, not the numeric value itself. You can add them when displaying the numbers later.
You can do something like this:
from ast import literal_eval
output = [int(i) for i in literal_eval("'0123','0124'")]
print(output)
The error occurs because a.replace(',', '') will only replace the , in the middle with an empty string, making it "'0123''0124'", now this still has the quotes to be handled of, that's why it raised the error.
You can split the string with the comma, and replace the single quotes with empty string and pass that to int(), like this...
a = "'0123','0124'"
b = tuple([int(i.replace("'", "")) for i in a.split(",")])
print(b)

Getting invalid literal for int() with base 10. while converting str into int

I am trying to convert a string into int but facing some difficulty. how to over come this?
random_image=['12.jpg']
s1=str(random_image)[1:-1].replace(".jpg", "")
s2=int(s1)
You first need to extract the file name from the list. That's easy as its the first and only item. Then you can slice off the last 4 chars of extension and convert to an integer:
random_image = ['12.jpg']
s1=int(random_image[0][:-4])
print(s1)
Output
12
str(random_image) evaluates to the string ['12.jpg'], so you need to strip two chars off the front and back before replacing the .jpg. Why are you explicitly calling str?
s1=str(random_image)[1:-1].replace("'",'').strip(".jpg")
s2=int(s1)
You can try this

Python: How do string variables prevent escape?

>>>m = "\frac{7x+5}{1+y^2}"
>>>print(m)
rac{7x+5}{1+y^2}
>>>print(r""+m)
rac{7x+5}{1+y^2}
>>>print(r"{}".format(m))
rac{7x+5}{1+y^2}
>>>print(repr(m))
'\x0crac{7x+5}{1+y^2}'
I want the result:"\frac{7x+5}{1+y^2}"
Must be a string variable!!!
You need the string literal that contains the slash to be a raw string.
m = r"\frac{7x+5}{1+y^2}"
Raw strings are just another way of writing strings. They aren't a different type. For example r"" is exactly the same as "" because there are no characters to escape, it doesn't produce some kind of raw empty string and adding it to another string changes nothing.
Another option is to add the escape sign to the escape sign to signify that it is a string literal
m = "\\frac{7x+5}{1+y^2}"
print(m)
print(r""+m)
print(r"{}".format(m))
print(repr(m))
A good place to start is to read the docs here. So you can use either the escape character "\" as here
>>> m = "\\frac{7x+5}{1+y^2}"
>>> print(m)
\frac{7x+5}{1+y^2}
or use string literals, which takes the string to be as is
>>> m = r"\frac{7x+5}{1+y^2}"
>>> print(m)
\frac{7x+5}{1+y^2}

how to convert this string to '380,00\xa0' to float,I get invalid literal for int() with base 10

I know there is many questions as mine and I tryied almost all of them but no one of them worked
Here is what I have '380,00\xa0' as a string want to convert it to float
But here is what I get ValueError: could not convert string to float: '380,00\xa0'
I tried str.replace("\xa0", "")
The length of the string is dynamique so I can't do float(str[:5])
The decimal separator for float is represented by the "." character, not by the "," character. Hence we have to do:
>>> s = "380,00\xa0"
>>> f = float(s.replace("\xa0", "").replace(",", "."))
>>> f
380.0

Convert string to integer in python

def handle_client_move(req):
strmove = req.decode('utf-8')
strmove = strmove[-1:]
sendmove = strmove.strip()
print(int(sendmove))
strmove = '--' + strmove
return(strmove)
I get this errror :
ValueError: invalid literal for int() with base 10: ''
cant convert strmove to integer.
To handle this specific problem, where you're trying to convert an empty string to an integer, you could do this:
int(strmove or 0)
When strmove is the empty string, which is falsey, strmove or 0 evaluates to 0 and that works fine as an argument to int(). You could also use some other number if that's more appropriate.
strmove[-1:] will give you only last symbol in your string. If it is whitespace then strmove.strip() will return empty string. Your error
ValueError: invalid literal for int() with base 10: ''
says that '' (empty string) is invalid literal for integer (which is true).
So depending on what you want, you probably need to strip() before strmove[-1:], or something else.

Categories

Resources