标签: f-string

1
推荐指数
1
解决办法
73
查看次数

试图理解一些f-string魔法(格式化f-string中的迷你语言)

这篇文章的评论中,有人放弃了这行代码:

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}}').

python python-3.x f-string

0
推荐指数
1
解决办法
118
查看次数

在Python 3.6+中,什么是f字符串才能将float 9.9打印为字符串09.90和10输出为10.00?

hHaving看着其他三个格式问题,这里有几十每个我没有看到任何人讲如何打印浮动的答案9.909.90,并1010.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在上面应该代替什么?

python python-3.x f-string

0
推荐指数
2
解决办法
45
查看次数

使用f字符串作为模板

以前,我用作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字符串,我很想知道是否有一种以这种方式使用它的方法。定义模板,并在需要时替换为适当的值,而不是立即评估括号的内容。

python python-3.x f-string

0
推荐指数
1
解决办法
52
查看次数

尝试在discord python bot中使用多个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)

python discord f-string discord.py

0
推荐指数
1
解决办法
147
查看次数

为整数月份和日期添加“0”的最简单方法是什么?

我有输入monthday都是int类型,我想将它们传递给函数来构造路径,如果monthday是低于 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

代码可以工作,但看起来很复杂,有没有更好的方法来实现这一点?

python if-statement f-string

0
推荐指数
1
解决办法
2924
查看次数

在 Python 中具有计算值的格式化字符串

在 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 string f-string

0
推荐指数
1
解决办法
77
查看次数

是否可以像 Python 的 f-strings 一样在 C 中格式化字符串?

在 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 中的打印语句的唯一方法吗?

c f-string

0
推荐指数
1
解决办法
100
查看次数

如何在列表中创建 f 字符串新行?

我正在尝试在列表中使用 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)

我怎样才能做到这一点?

python f-string

0
推荐指数
1
解决办法
185
查看次数

如何在Python中使用f字符串将变量用作字典键?

我正在尝试打印字典的字典项目值,但我的项目也是另一个用户输入变量。如何使用 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)

python dictionary associative-array f-string

-1
推荐指数
1
解决办法
1525
查看次数

如何在Python中将空字符串传递给三引号f-string?

这是一个使用带有大量子元素的三引号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''

python lxml string-literals f-string

-3
推荐指数
1
解决办法
60
查看次数