This question already has answers here:
Transform string to f-string
(5 answers)
Closed 2 years ago.
I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically.
Example
Assigned from JSON I get
whose_fault= "{name} started this whole mess"
How to build a lambda to convert it to an f-string and insert the given variable? I just can't quite get my head around it.
I know similar questions have been asked, but no answer seems to quite work for this.
Better question. What's the most pythonic way to insert a variable into a string (which cannot be initially created as an f-string)?
My goal would be a lambda function if possible.
The point being to insert the same variable into whatever string is given where indicated said string.
There is no such thing as f-string type object in python. Its just a feature to allow you execute a code and format a string.
So if you have a variable x= 2020, then you can create another string that contains the variable x in it. Like
y = f"It is now {x+1}". Now y is a string, not a new object type,not a function
Related
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}"
This question already has answers here:
How do I split the definition of a long string over multiple lines?
(30 answers)
How can I split up a long f-string in Python?
(2 answers)
Closed 10 months ago.
I'm building a rather long file path, like so:
file_path = f"{ENV_VAR}/my_dir/{foo['a']}/{foo['b']}/{bar.date()}/{foo['c']}.json"
This is a simplified example. The actual path is much longer.
To make this line shorter and more readable in code, I have tried the following:
file_path = f"{ENV_VAR}/my_dir\
/{foo['a']}\
/{foo['b']}\
/{bar.date()}\
/{foo['c']}.json"
This works but also affects the actual string in my program.
More specifically, the linebreaks are added to the string value itself, which is undesirable in this case. I only want to change the formatting of the source code.
Is it possible to format the string without affecting the actual value in my program?
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has answers here:
Format align using a variable?
(2 answers)
Closed 4 years ago.
I am still trying to understand the new syntax for string formatting in Python.
"{fpath:>80}".format(fpath=path, flongest=longest)
The above code interpolates the path argument, and formats the string correctly, but replacing the text window value (80) with a variable gives
ValueError: Invalid format specifier
"{fpath:>flongest}".format(fpath=path, flongest=longest)
How can the window size be assigned through a variable?
"{fpath:>???}".format(fpath=path, flongest=longest)
Okay, finally got it. The var 'longest' can be a string version of a number ('80') or an int (80). The trick is using curly brackets around only the right interpolated var.
"{fpath:>{flong}}".format(fpath=path, flong=longest)
Try to use this:
longest='{0}.format(variable value)'#let say 80
print('{fpath:>{flong}}'.format(fpath=path,flong=longest))
the variable length also needed to be converted into format specifier. I hope you understand. If still there is any problem then please let me know.
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])