Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
eg-In this instead of using 21 (a value), I want to use a variable to generalize it
print("{:-^21}".format(".|."*(2*(i+1)-1)))
I want to use something like this
print("{:-^M}".format(".|."*(2*(i+1)-1)))
That can easily enough be done. For example:
M = 40
i = 3
print("{val:-^{width}}".format(width=M, val=".|."*(2*(i+1)-1)))
Outputs:
---------.|..|..|..|..|..|..|.----------
You could also do it with f-strings (note the outer ' because " is used on the inner expression):
print(f'{".|."*(2*(i+1)-1):-^{M}}')
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Like if I type in something in an input prompt, I want to search my modules for a variable or something to reference, like so:
variable_caller = input().function_here
# or:
variable_caller.function_here
Any ideas?
You can use getattr(YOUR_MODULE, variable_selection)
getattr() returns value of YOUR_MODULE."variable_selection".
It gets the value of variable variable_selection and converts it into ID.
For example:
a = "attribute"
getattr(module, a) # Returns module.attribute
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'd like to convert a number like 1323.67 to 1.323,67, how can I do that?
I've tried this
f'{1325.76:.,2f}'
but it prints out 1,325.76
I excpected f'{1325.76:.,2f}' to be 1.325,75 but it's 1,325.76
If you can use external modules, I would suggest you to use babel
>>> from babel.numbers import format_decimal
>>> format_decimal(1323.67, locale='de_DE')
'1.323,67'
The format is
f'{1325.76:,.2f}' and not
f'{1325.76:.,2f}'
:,.2f is what you want. Which means , as separator with 2 decimal positions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to convert a tuple:
('Cobra',)
to a string, which when printed yields:
Cobra
#Assuming you have a list of tuples
sample = [('cobra',),('Cat',),('Dog',),('hello',),('Cobra',)]
#For each tuple in the list, Get the first element of each tuple
x = [i[0] for i in sample]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I took a python course back when i was in high school but now I barely remember anything about it. I'm bored today and though I should try some python exercises.
Example:
string = '3dc8uo8c33a v8c08oizl6ga'
The code needs to remove 3d 8u 8c ... ect
so that the
answer = 'coca cola'
Assuming the rule is "split the string along whitespace, then take every third letter of the words, and add them back together", you can use
>>> string = '3dc8uo8cc33a v8c08oizl6ga'
>>> " ".join("".join(s[2::3]) for s in string.split())
'coca cola'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can some one suggest equivalent code in python for below line
UInt32.Parse(("000000" + hexfileln.Substring(1, 2)), NumberStyles.HexNumber);
It is pointless to prefix a hex number with zeros. Ignoring that, and noting that the int constructor accepts a string argument and the base, you can use int to parse a hexadecimal number:
int(hexfileln[1:3], 16)