Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

How can I re-format using the f-string this string:

string_from_db = "Hello {full_name}"

full_name = "Mark X"

return f'{string_from_db}'

This example is not working, I have the string which is ready for the params injections in the DB, And I want to use the python 3.6 f-string on that data.

1 Answer

0 votes
by (36.8k points)
edited by

You could use a similar pre f-string approach:

def my_function():

    string_from_db = "Hello {full_name}"

    full_name = "Mark X"

    return string_from_db.format(**locals())

print(my_function())

Or the more standard approach:

def my_function():

    string_from_db = "Hello {full_name}"

    full_name = "Mark X"

    return string_from_db.format(full_name=full_name)

print(my_function())

Learn Data Science with Python Course to improve your technical knowledge. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...