user1554264
user1554264

Reputation: 1224

Python - Checking how many instances of strings are embedded within strings

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.

Upvotes: 0

Views: 37

Answers (1)

Nordle
Nordle

Reputation: 2981

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.

Upvotes: 1

Related Questions