Python检查字符串中嵌入了多少个字符串实例

2024-07-06 23:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下Python脚本:

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)

在字符串中嵌入了多少个字符串实例

我问这个问题的原因是因为我正在学习,并且被告知要对这些实例进行计数,我认为其中有4个实例。然而,教学资源继续说,可能有4个以上的例子

如果措辞模棱两可或术语不当,则表示歉意。我在学习这门语言的同时尽量使事情简单化

我的理解是有4个,我已经在脚本中对其进行了评论。不过,我相信还会有更多,如果可能,请告知,我们将非常感谢您的帮助


Tags: of实例instance脚本notpeopledotypes
1条回答
网友
1楼 · 发布于 2024-07-06 23:41:09

这就是我得到的

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)

f字符串(f'foobar {variable}')或使用.format()函数('foobar {0}'.format(variable))后的字符串的任何实例都在字符串中嵌入字符串

相关问题 更多 >