【问题标题】:Dynamically Creating u-strings in Python在 Python 中动态创建 u-strings
【发布时间】:2021-05-20 19:25:12
【问题描述】:

https://docs.python.org/2/tutorial/introduction.html#unicode-strings

我目前正在尝试弄清楚如何动态创建 u-strings(例如,以 u'helloWorld' 的形式)。如果可能的话,我想创建一个带有字符串连接或字符串注入的 u-string,以使用变量动态创建一个新的 u-string,例如 u'{variableName}'。有没有办法做到这一点?

Python 2 u-strings 的使用是为了使用 gcloud 函数,我试图在其中动态添加文档。该文档使用 u-strings,可以在以下位置找到:

https://firebase.google.com/docs/firestore/manage-data/add-data

【问题讨论】:

  • 字符串与 unicode 字符串仅在 Python 2 中有意义,而 Python 2 在 2021 年很少有意义。您想要完成什么? (公平地说,Apple 将 Python 2 设为默认值太久了,这让事情变得更加混乱。
  • 你真的在使用 Python 2 吗?在 Python 3 中,所有字符串都是 Unicode。它们的管理方式与常规字符串一样。
  • 你为什么要编写 Python 2 教程
  • 您尝试过哪些不起作用的方法? mys = u"one" + u"two" 在 Python 2 中应该可以正常工作。
  • Python 2 达到了它的end of life on January 1, 2020。如果我没记错的话,所有字符串都是u-strings by default in Python 3

标签: python-2.x


【解决方案1】:

u-strings 在 Python 3 中不再存在。它们与普通字符串相同。所以只需使用普通字符串。

# Python 3
>>> u'Hello'
'Hello'

不要使用 Python 2,因为它不再受支持。如果您坚持使用 Python 2,请尝试:

# Python 2
>>> unicode('Hello')
u'Hello'

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 2012-06-11
  • 2018-04-15
  • 1970-01-01
  • 2011-04-24
相关资源
最近更新 更多