>>> str(42)
'42'
>>> int('42')
42
Links to the documentation:
int()str()
str(x) converts any object x to a string by calling x.__str__(), or repr(x) if x doesn't have a __str__() method.
Cannot use str() to convert an int to a string.
Quick question: Is there a way to get the first three digits of a int without converting it to a string or list?
You probably shouldn't be keeping phone numbers as ints, because they don't behave like ints. You'll never add, subtract, multiply, or divide phone numbers. You will, however, access individual digits of them, so an array is a much more appropriate type. If you absolutely must store them as ints for efficiency reasons (unlikely), then at point of use you should probably just convert them, unless that is too expensive (even more unlikely).
Remember readability is generally more important than minor performance gains, so digits(num)[:3] is much easier to understand than (num-num%le6)/le6 as skier_scott suggests doing.
More on reddit.comHow to convert scientific notation string to integer?
Do you normally use string.format() or percentage (%) to format your Python strings?
Firstly, you can write e. g. {0:.2f} to specify a float with 2 decimals, see e. g. https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3
Secondly, the best formatting method is f-strings, see e. g. https://www.blog.pythonlibrary.org/2018/03/13/python-3-an-intro-to-f-strings/
More on reddit.comVideos
my_input = int(my_input)
There is no shorter way than using the int function (as you mention)
Maybe you were hoping for something like my_number = my_input.to_int. But it is not currently possible to do it natively. And funny enough, if you want to extract the integer part from a float-like string, you have to convert to float first, and then to int. Or else you get ValueError: invalid literal for int().
The robust way:
my_input = int(float(my_input))
For example:
>>> nb = "88.8"
>>> int(nb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '88.8'
>>> int(float(nb))
88
Okay, so I had a crash course in Python a looooong time ago, and that's all the prior experience I have with it. I'm starting to dabble in it again, playing around with it to make a text-based game.
In this game, you have stats - Speed, Health, etc etc. Each of these is a number (an int).
I am trying to define a function names statdisplay() so that when I call on it, it prints out your stats. So, if you have a Speed of 1, and the max Speed you can increase that stat to is 5, one of the printed lines would be:
Speed: 1 / 5
This was my ORIGINAL code:
print("\nHealth: " + healthstat + " / " + maxhealth)
print("\nHunger: " + hungerstat + " / " + maxhunger)
print("\nStalking Skill: " + stalkingstat + " / 5")
print("\nHunting Skill: " + huntingstat + " / 5")
print("\nSpeed: " + speedstat + " / 5")
print("\nStrength: " + speedstat + " / 5")But then I got the following error:
TypeError: can only concatenate str (not "int") to str
So I mentally facepalmed myself and looked up how to convert an int to a string, and I keep reading that you can use the str() function (I know there are other ways, but I'm taking baby steps here trying to jog my memory on how everything works before I go doing everything by what's deemed "most appropriate").
This is my NEW code with that in mind:
print("\nHealth: " + str(healthstat) + " / " + maxhealth)
print("\nHunger: " + str(hungerstat) + " / " + maxhunger)
print("\nStalking Skill: " + str(stalkingstat) + " / 5")
print("\nHunting Skill: " + str(huntingstat) + " / 5")
print("\nSpeed: " + str(speedstat) + " / 5")
print("\nStrength: " + str(speedstat) + " / 5")...and yet I am still getting the following error:
TypeError: can only concatenate str (not "int") to str
I can't seem to figure out what's wrong. I do not have the str() function defined as anything else. str() also doesn't seem to come from any special libraries that I'd need to import, but maybe I'm wrong there (I tried to look it up)... the only import I currently have is "import random".
My only other thought is that maybe it's a Google Colab thing, as that's where I'm currently running my code since this wasn't anything serious and I tend to go between two different computers.
Any help would be much appreciated!
Edit: accidentally had the new code in both code boxes.