How to print string variable as f string in python [duplicate] - python

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)

Related

Can we use a variable to create a new variable in Python? [duplicate]

This question already has answers here:
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 5 months ago.
I am wondering if we can use a variable name in order to create a new variable, for example:
Let's assume I have this variable:
Var = 'Jim'
Lets say I want to concatenate the variable with a string, in this case the string is the word Mr:
NewVar = "String"Var
So that if I print the new variable, the output would look something like:
MrJim
This can be achieved in bash like this:
NewVar=Mr${Var}
But I have not found a way to do this in Python. Please let me know if you know how to do it.
Have a look at python string interpolation.
var = "Jim"
new_var = f"Mr {var}"

Saving a file name as a input variable [duplicate]

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")

String format: getting u'' inside the final string [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')

python padding formatting alternative? [duplicate]

This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 5 years ago.
Is there a custom way of padding lines of text in python, I am using the escape characters "\t", but I wonder if there is an alternative.
for example
print('My Name is:')
print('Rambo')
print('Mambo')
Output:
.My Name is:
.....Rambo
..Mambo
Try using:
print('{:>15}'.format('My Name is:'))
Refer for examples:
PyFormat
Write a simple function for yourself.
def p(a,b):
print(" "*a + b)
p(1,"while")
This should return:" while"

python dict in formatted string [duplicate]

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 7 years ago.
I want to write a python dictionary in a string. The values of the items should be give by the formatted string.
I expected the following to work, but it doesnt because the first '{' used to define the dict is interpreted as a formated field
In: "{'key_1': '{value}'}".format(**{'value': 'test'})
the following works fine, but doesn't return the expected dict:
In: "'key_1': '{value}'".format(**{'value': 'test'})
Out: "'key_1': 'test'"
one way to overcome the problem is by using list brakets and replace them later by the dict brakets:
In: "['key_1': '{value}']".format(**{'value': 'test'}).replace('[', '{').replace(']', '}')
Out: "{'key_1': 'test'}"
How to write this in a better way?
Use double {{and }}:
"{{'key_1': '{value}'}}".format(**{'value': 'test'})

Categories

Resources