This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Putting a variable into a string (quote)
(4 answers)
Closed 2 years ago.
very new to python so sorry for the silly question
I've created a user input interface
fn=input()
#Where the user will input version32 for instance so effectively
fn=Version32
I've then imported a template word document using docx, which has been heavily modified based upon user input. I then want the file name output to be saved as "fn" or in this case Version32
output.save(r"C:\Users\XXX\XXX\XXX\'fn'.docx")
Where fn is a variable? Is this even possible, or am I barking up the wrong tree?
Kind Regards!!
IIUC, you can do this using f-string:
output.save(rf"C:\Users\XXX\XXX\XXX\{fn}.docx")
Related
This question already has answers here:
What determines which strings are interned and when? [duplicate]
(3 answers)
About the changing id of an immutable string
(5 answers)
Closed 9 months ago.
I am fairly ok with Python, but recently I learned something that made me question everything I thought to be true in my life :D
So if I run this:
w1 = "word"
w2 = "word"
print(id(w1))
print(id(w2))
To my utter horror, the output is:
140675515277936
140675515277936
I always believed that as strings are immutable, they will have their own address. I was wrong. My question then:
How does the Python Memory Manager find existing string (or any) objects when a new variable is created, so it can make that new variable point to them?
With some hashing? If besides your answer you can point me to learning material about this, i will be double grateful! Cheers!
This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.
This question already has answers here:
How do I convert a string into an f-string?
(3 answers)
Closed 2 years ago.
i want to print my user variable from my python script to a html document and this is the way I have to do this.
How do i go about doing it?
my_python.py
user = "myname"
with open("../../html/forms/output.html") as output:
print(output.readlines())
output.html
<h1>{user}</h1>
EXPECTED OUTPUT :
<h1>myname</h1>
ACTUAL OUTPUT :
<h1>{user}</h1>
f-string is a syntax and not an object, and as a result - You can't convert a string to f-string.
If you know the names of the "variables" inside your template file, you can do:
output.read().format(user=user)
This question already has answers here:
Python String Formatting - Type Error - Not enough arguments for format string
(2 answers)
Closed 3 years ago.
so for my problem, I have to create a program that takes a users input, and then prints it like a list. For example, if the user inputs "I like doing this activity, the program should return it as.
I
like
doing
this
activity.
But these should work with any scenario that the user inputs.
I tried using the input function and split to try and define the variables
my_list_of_data = input("Enter your sentence:").split()
print("(%s),/n(%s),/n(%s),/n(%s),/n(%s)"%(my_list_of_data))
I am getting an error in line 2, saying there is not enough arguments to create a string.
Print the list like this:
print('\n'.join(my_list_of_data))
This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)