首页 > 解决方案 > 在 Python 中动态创建 u-strings

问题描述

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

标签: python-2.x

解决方案


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

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

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

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

推荐阅读