python|f-string and quotation mark

本文详细介绍了Python3的f-Strings,这是一种更方便的字符串格式化方法。通过使用f前缀和花括号包围变量,可以直接在字符串中嵌入变量值。例如:`f'Iam{variable}

Python 3's f-Strings: An Improved String Formatting Syntax (Guide) – Real Python

The quotation mark shouldn't be the same.


x=int(input())
print(f'W{x*"o"}w')
#it doesn't work if the quotation mark is the same
>>> f'I am {"Huang Wei"}'
'I am Huang Wei'

>>> f'''I am {'Huang Wei'}'''
'I am Huang Wei'

>>> f"""I am {'Huang Wei'}"""
'I am Huang Wei'

>>> f"""I am {"Huang Wei"}"""
'I am Huang Wei'

>>> f'I am {'Huang Wei'}'
  File "<stdin>", line 1
    f'I am {'Huang Wei'}'
                 ^
SyntaxError: invalid syntax

python格式化输出(二):f-string格式化输出_数据分析与统计学之美-优快云博客

AttributeError Traceback (most recent call last) Cell In[32], line 4 2 table_color="blue" 3 table = Table() ----> 4 table.add(headers=[v_title], rows=[], attributes={ 5 "align": "center", 6 "border": False, 7 "padding": "2px", 8 "style": "background:{}; width:1350px; height:50px; font-size:25px; color:#C0C0C0;".format(table_color) 9 }) 10 table.render('大标题.html') 11 print('生成完毕:大标题.html') File ~\anaconda3\Lib\site-packages\pyecharts\components\table.py:30, in Table.add(self, headers, rows, attributes) 28 for r in rows: 29 table.add_row(r) ---> 30 self.html_content = table.get_html_string() 31 return self File ~\anaconda3\Lib\site-packages\prettytable\prettytable.py:2490, in PrettyTable.get_html_string(self, **kwargs) 2488 string = self._get_formatted_html_string(options) 2489 else: -> 2490 string = self._get_simple_html_string(options) 2492 return string File ~\anaconda3\Lib\site-packages\prettytable\prettytable.py:2506, in PrettyTable._get_simple_html_string(self, options) 2504 if options["attributes"]: 2505 for attr_name, attr_value in options["attributes"].items(): -> 2506 open_tag.append(f' {escape(attr_name)}="{escape(attr_value)}"') 2507 open_tag.append(">") 2508 lines.append("".join(open_tag)) File ~\anaconda3\Lib\html\__init__.py:19, in escape(s, quote) 12 def escape(s, quote=True): 13 """ 14 Replace special characters "&", "<" and ">" to HTML-safe sequences. 15 If the optional flag quote is true (the default), the quotation mark 16 characters, both double quote (") and single quote (') characters are also 17 translated. 18 """ ---> 19 s = s.replace("&", "&") # Must be done first! 20 s = s.replace("<", "<") 21 s = s.replace(">", ">") AttributeError: 'bool' object has no attribute 'replace'
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值