str.format() with both alignment {:>} and variable interpolation [duplicate] - python

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.

Related

From string to fstring [duplicate]

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

How to convert a string containing hexadecimal number to ascii text in python [duplicate]

This question already has answers here:
Convert from ASCII string encoded in Hex to plain ASCII?
(9 answers)
Closed 4 years ago.
I've been trying to convert this string:
str = "68656c6c6f20776f726421"
to recover a ASCII value:
str = "hello word!"
I need some help with this please.
EDIT
Sorry for not giving more information, I'm a newbie, unfortunately.
but reading several pages of this wonderful site I found the solution.
the problem was that I got the string of a file, and a \n was printed, changing the length of the string.
Solution here: Python: binascii.a2b_hex gives "Odd-length string"
have you tried
codecs.decode(str, "hex").decode('utf-8')
You may need to assign this to a variable or print the results to see the output depending on your use case.
If I was doing this solution it would be the following:
data = bytes.fromhex(b'68656c6c6f20776f726421'.decode("ascii"))
print('The hex value converted to ascii is the following:',data.decode('utf-8'))

How do I turn a list into a string [duplicate]

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'])

Is there a way to infuse the input variable in regex? [duplicate]

This question already has an answer here:
use variable inside regex python
(1 answer)
Closed 5 years ago.
EDIT: So apparently, this is a string interpolation in regex. Thanks for clarifying.
I have a input function that is named omj.
omj
When I run it, it gives me
"obik"
I then use that output in this regex function
re.findall("\w*obik\w*",dataframe)
I received EXACTLY what I wanted, which is the answer
"Yaobikuni"
Notice that "obik" is in the word, and there is only one match for it. Is there a way to put the input omj in the regex function to get Yaobikuni straightforwardly, or is this the only way it would work?
EDIT: I don't understand why people are downvoting, but I thought I made it clear that omj can be considered as an input string that gives the answer obik.
omj = """obik"""
EDIT2: Thank you for the help #Nick Chapman . I tried this on the first time and I thought it might not have been possible to infuse the input omj on an regex function:
re.findall("\w*"omj"\w*",dataframe)
The first argument is just a string, so your real question is how to do string interpolation in Python.
How about this:
re.findall("\w*{}\w*".format(omj()), dataframe)
Can't you just do
re.findall("\w*" + omj + "\w*",dataframe)

What does a comma after an opened bracket mean? [duplicate]

This question already has answers here:
Why do Python function docs include the comma after the bracket for optional args?
(5 answers)
Closed 6 years ago.
Since the first times I started to study Python I met many schematic codes such as
pickle.dump(obj, file[, protocol])
Now in this example I can understand the meaning of the first comma, as it separate two different arguments to be inserted in a method, but I don't understand the second comma that is located after a square bracket.
Is there anyone who can explain me the meaning of this comma?
it's a common notation for indicating the next argument is optional. so you could write:
pickle.dump(obj, file)
or you could write:
pickle.dump(obj, file, protocol)
if you see angle brackets like <foo>, that is used to indicate the argument is required.

Categories

Resources