两个都
x = 1
f"{x}"
Run Code Online (Sandbox Code Playgroud)
和
x = '1'
f"{x}"
Run Code Online (Sandbox Code Playgroud)
给
'1'
Run Code Online (Sandbox Code Playgroud)
作为输出。我如何获得"1"and"'1'"作为输出?
在这篇文章的评论中,有人放弃了这行代码:
print("\n".join(f'{a:{a}<{a}}' for a in range(1,10)))
Run Code Online (Sandbox Code Playgroud)
1
22
333
4444
55555
666666
7777777
88888888
999999999
Run Code Online (Sandbox Code Playgroud)
它对我来说看起来很神奇,有人可以向我解释它为什么有效(更具体地说f'{a:{a}<{a}}').
hHaving看着其他三个格式问题,这里有几十每个我没有看到任何人讲如何打印浮动的答案9.9是09.90,并10为10.00使用相同的F-字符串:
x = 9.9
print(f"{x:02.2f}") // should print 09.90
x = 10
print(f"{x:02.2f}") // should print 10.00
Run Code Online (Sandbox Code Playgroud)
问题是:02.2f在上面应该代替什么?
以前,我用作str.format()模板方法,例如:
template = "Hello, my name is {0} and I'm {1} years old, my favourite colour is {2}"
# Rest of the program...
print(template.format("John", "30", "Red"))
Run Code Online (Sandbox Code Playgroud)
我最近了解了f字符串,我很想知道是否有一种以这种方式使用它的方法。定义模板,并在需要时替换为适当的值,而不是立即评估括号的内容。
您好,所以我已经尝试解决这个问题几个小时了,但无法弄清楚出了什么问题,我之前在 discord python 中进行了 api 调用,但这是第一次使用这样的多个变量。
@client.command()
async def random(ctx):
url = ('https://randomuser.me/api/')
response = requests.get(url)
title = response.json()["title"]
first = response.json()["first"]
last = response.json()["last"]
number = response.json()["number"]
street = response.json()["name"]
city = response.json()["city"]
state = response.json()["state"]
postcode = response.json()["postcode"]
country = response.json()["country"]
phone = response.json()["phone"]
age = response.json()["age"]
dob = response.json()["date"]
gender = response.json()["gender"]
username = response.json()["username"]
password = response.json()["password"]
image = response.json()['large']
embed = discord.Embed(title="Random Generator", description="**Name:** f'{title}, {first} {last}\n**Address:** {number} {street}, {city}, {state}, {postcode}, {country}\n**Phone:** {phone}\n**Age:** {age}, …Run Code Online (Sandbox Code Playgroud) 我有输入month和day都是int类型,我想将它们传递给函数来构造路径,如果month或day是低于 10 的数字,我们0在数字前面添加一个:
def construct_path(year, month, day):
if month >= 10 or day >= 10:
path = f"xxx/{year}/{month}/{day}"
elif month < 10 and day >= 10:
path = f"xxx/{year}/0{month}/{day}"
elif month >=10 and day <10:
path = f"xxx/{year}/{month}/0{day}"
else:
path = f"xxx/{year}/0{month}/0{day}"
return path
Run Code Online (Sandbox Code Playgroud)
所以construct_path(2021, 5, 2)应该返回xxx/2021/05/02。
代码可以工作,但看起来很复杂,有没有更好的方法来实现这一点?
在 JavaScript 中,我可以使用模板文字并包含计算值。例如:
var a = 3;
var b = 8;
var text = `Adding ${a} + ${b} = ${a+b}`; // Adding 3 + 8 = 11
Run Code Online (Sandbox Code Playgroud)
我知道 python 有f'…'字符串和str.format()占位符。有没有办法可以在字符串中包含计算?
在 Python 中,可以使用 f-strings 方便地格式化字符串:
num = 12
print(f"num is {num}") # prints "num is 12"
Run Code Online (Sandbox Code Playgroud)
有可能在 C 中做这样的事情吗?或者类似的东西?目前,要将变量添加到输出中,我正在使用此方法:
int num = 12;
printf("num is %d", num);
Run Code Online (Sandbox Code Playgroud)
这是将变量添加到 C 中的打印语句的唯一方法吗?
我正在尝试在列表中使用 f 字符串。我创建了一个新的行变量。标准变量工作正常,但新行变量不行
NL = '\n'
var = 'xyz'
lst = []
print(f'test{NL}new line')
lst.append(f"first line var is {var}{NL}a second line {NL}")
lst.append(f"third line{NL}forth line var is {var}")
print(lst)
Run Code Online (Sandbox Code Playgroud)
创建输出
test
new line
['first line var is xyz\na second line \n', 'third line\nforth line var is xyz']
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试打印字典的字典项目值,但我的项目也是另一个用户输入变量。如何使用 f 字符串打印选定的值?
option = ''
languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++', '4':'Learn PHP', '5':'Quit'}
option = input('Please choose an option between 1 and 5')
# Following part is not working!!!
print(f'Youve selected {languages["{option}"]}')
Run Code Online (Sandbox Code Playgroud) 这是一个使用带有大量子元素的三引号f-string的函数:
def pass_empty_string(param):
from lxml import etree
xml = etree.XML(f'''
<root>
<child>text</child>
<child>{param}</child>
...
</root>''')
return xml
Run Code Online (Sandbox Code Playgroud)
获得或重视</child>时param是否可以获得空元素?None''
f-string ×11
python ×10
python-3.x ×3
c ×1
dictionary ×1
discord ×1
discord.py ×1
if-statement ×1
lxml ×1
string ×1