This question already has answers here:
Type Error: Format Requires Mapping
(2 answers)
Closed 3 years ago.
text1="Python"
text2="with me"
print("Study %(language)s" %{'language':text1})
This works. But I am wondering whether it is using dictionary to call string?
print("Study %(language)s %(with whom)" %({'language':text1},{'with whom':text2}))
But it doesn't work. How can I fix it?
The error says 'format requires a mapping'
It would have worked, if you noticed that:
you've forgotten to put an s after %(with whom) --> %(with whom)s,
and instead of this %({'language':text1},{'with whom':text2}) --> %{'language':text1,'with whom':text2}
so the line would be like this:
print("Study %(language)s %(with whom)s" %{'language':text1,'with whom':text2})
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed last month.
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'
This question already has answers here:
python - name of np array variable as string
(2 answers)
Closed 9 months ago.
If I wish to get the name of a numpy.ndarray converted to a string (but not the content of the numpy.ndarray, just the name only), how do I do that?
I tried str(npndarrayName) and npndarrayName.tostring() but both are converting the content and not the name itself only.
I am not sure if I fully understood what you're asking, but you can check this link because I think they had the same question as you.
I hope that it will solve your problem.
This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])
This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Add new statements to Python without customizing the compiler
(2 answers)
Closed 4 years ago.
Can I create a statement in python 2.7?
Something similar to the print statement, that it's like a function, but gets the parameters without parenthesis.
This question already has answers here:
pep8 compliant deep dictionary access
(2 answers)
Closed 4 years ago.
I have a line accessing a very nested attribute in a dictionary that is now failing a PEP8 checker that was recently added to our release pipeline.
For example:
nested_attr = d['long_attr_name_1']['long_attr_name_2']['long_attr_name_3']['you_get_the_point']
What is the pythonic way to break up a line like this so it does not exceed the style guide line limit check?
nested_attr = (d['long_attr_name_1']['long_attr_name_2']
['long_attr_name_3']['you_get_the_point'])
Wrapping in brackets if a PEP8 saviour!