0

Given the following Python script:

types_of_people = 10
x = f"There are {types_of_people} types of people."

binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}." # 2 instances

print(x)
print(y)

print(f"I said: {x}") # 1 instance
print(f"I also said: '{y}'") # 1 instance

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))

w = "This is the left side of..."
e = "a string with a right side."

print(w + e)

How many instances of strings embedded within strings occur?

The reason I am asking because I'm learning and was told to perform a count of these instances, which I think there are 4 of. However the teaching resource has gone on to say there could be more than 4 instances.

Apologies if the phrasing seems ambiguous, or terminology seems off. I'm trying to keep things simple while I learn the language.

My understanding is that there are 4, which I have commented in the script. However I believe there could be more, please advise if possible, help would be much appreciated.

1 Answer 1

1

This is what I get;

types_of_people = 10
x = f"There are {types_of_people} types of people." #First instance

binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}." #Second and third instances

print(x)
print(y)

print(f"I said: {x}") # #Fourth instance
print(f"I also said: '{y}'") #Fifth instance

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}" #Sixth instance (shared with below line)

print(joke_evaluation.format(hilarious)) #Shared sixth instance

w = "This is the left side of..."
e = "a string with a right side."

print(w + e)

Any instances of an f string (f'foobar {variable}') or a string succeded with the .format() function ('foobar {0}'.format(variable)) are embedding strings within string.

Sign up to request clarification or add additional context in comments.

5 Comments

Hang on a second - types_of_people is an integer data type, so therefore your first instance is incorrect - this is an int within a string surely? Or is the case that I'm mistaken and the int is converted to a string when it is output?
You are right that it's an integer, but what f strings do is evaulate any calculations within the {} brackets and then automatically converts the result to string.
Ah alright thank you, and finally in your sixth instance the hilarious variable that is a boolean is also converted to a string when formatted?
consider the following f string: f'Five plus five is: {5 + 5}', python evaluates the calculation 5+5, and returns the result as a string '10'
Exactly correct! Also a side note, you need 3.6+ in order to use f strings as they were not introduced in earlier versions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.