Python String Expression format [closed] - python

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 am not understanding the meaning of each of the terms in the curly brackets (I was a bit overwhelmed with this source https://docs.python.org/3.4/library/string.html)
def format_money(amount):
return '${:.2f}'.format(amount)
I know that it return this format: $39.99 but I don't know what each sign means. Could someone explain?

the $ is outside of formatted string placeholder
{: is the beginning of the formatted string
.2f is the fixed number of places after the decimal
} end of your place holder
i find it always fun to experiment to test a function or a module, you can also format the date, output, lots of other stuff... all in that lovely reference page

Related

New into programming [closed]

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 days ago.
Improve this question
How do I start a code in Python? Is there any formula/rule or so I can or have to use/follow?
I have read so much about all different types and functions in python, and still am not able to simply start with a code, although I know in my mind, what is to be considered.
I tried to start, there where error messages I didn't understand, then I went to ChatGPT and all my ideas were most of the time correct, but I can't create the right form of the code.
And somehow I am not able to get it out of all the stuff I already read.

How to identify common substrings of two strings in Python? [closed]

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 1 year ago.
Improve this question
How can I find common substrings of two strings in order to show changes/edit of a string?
So what I try to do is to compare an old version of a string:
string_old = "My name is pm730! How are you?"
with a new/edited version of the string:
string_new = "My name isn't pm730, it is pm740!"
Deleted substrings are not important. New substrings should be distinguished somehow, so that I could output it like this eventually:
My name isn't pm730 , it is pm740!
This task sounds easy but is more complicated than I thought. So my hope is that there is already an similar implementation available, but unfortunanly I can't find it...

Filtering string column with specific character [closed]

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 1 year ago.
Improve this question
I have a string column like this and want to filter content between "/" character:
A
9/17/001.a.x.y.16.04451
006.b.021017006814
2/17/000.c.m.n.15.00668/008
And the expected output is
A
001.a.x.y.16.04451
006.b.021017006814
000.c.m.n.15.00668
How could i make it done with python/R/Mysql
Thank youuu
In MySQL, you can use regexp_replace():
select t.*,
regexp_replace(a, '^[^/]+/[^/]+/([^/]*)[/|^]', '$1')
from t;
The logic is that you seem to want the third component between slashes if there is one. Otherwise, you seem to want the entire string.
Here is a db<>fiddle.

How can I write this in one line? [closed]

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
Hey So I'm working on a problem that asks me to manipulate a dictionary by grabbing all the keys and values and formatting them into a single string for every key/value pair. I have to do this in one line, or so I think, the problem is better explained in the image.
I believe they would like you to write for country, pop in country_pop.items(): on line 7. However, you can do the entire thing in one line as well.
print('\n'.join([f'{key} has {value} people' for key, value in country_pop.items()]))

Why is Python strict about indentation? [closed]

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 8 years ago.
Improve this question
This question is coming from this Stack Overflow question. Why is Python is designed to use correct indentation either space or tab? In this way, is it purposefully to serve any benefit?
Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.
Consider this code snippet, for example:
def f(x):
if x == 0:
print("x is zero")
print("where am i?")
If it weren't for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it under the if statement, the function f or just by itself in the document?
There is no set width of a tab character, so Python has no way of knowing how many spaces to count a tab as.

Categories