This question already has answers here:
How to insert a variable value in a string in python
(3 answers)
Closed 1 year ago.
So, I want to put a random number as my filename in f=open
I generated the number with d=random.randint(1,10)
and I want to put that as my filename in f=open.
f=open("test.txt", "x")
How can I do that?
I don't think you tried very hard to solve this yourself.
d = random.randint(1,10)
f = open(f"test{d}.txt",'w')
Related
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:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.
This question already has answers here:
How do you print superscript in Python?
(13 answers)
Closed 4 years ago.
I'm doing a maths revision program and I want to print out a question with y to the power of x(like xⁿ). I can't find a way to print it out as this form. Has anyone got any ideas?
This is how I want it to be displayed.
One possible option using ANSI escape sequences, it is not possible to control font size:
base = "x"
exponent = "n"
print('\033[1BFormula = %s\033[1A%s\033[1B etcetera'%(exponent, base))
In terminal you get:
# x
#Formula = n etcetera
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 can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
Tools_List=["Hoe","Pitchfork","Shovel"] #`list`
print(Tools_List)
Tools=input("What tools do you want? ")
print(Tools_List[Tools])
One can only index with one index as a time, wheres your prompt asks for 'Tools' plural. I suggest (with the int correction, and lower-case names)
tools = ["Hoe","Pitchfork","Shovel"]
print(tools)
tool = int(input("What tool do you want? "))
print(tools[tool])