1、当标准字符串和Unicode字符串在表达式中混合用时,标准字符串将被自动转换为Unicode字符串。例如:
s = "hello"
t = u"world"
w = s + t #w = unicode(s) + t
2、当一个字符串方法中使用到Unicode字符串时,结果也总将是Unicode字符串,例如:
a = "Hello World"
b = a.replace("World",u"Bob") #b = u"Hello Bob"
此外,使用replace()方法进行零替换(结果仍是原始字符)时,结果仍然会是Unicode字符串。
3、如果一个Unicode字符串使用%操作符做格式字符串,所有参数在一开始都将强制换换位Unicode,然后再根据格式规则连接在一起,如果Unicode对象被用作%操作符的一个参数,整个结果也将是Unicode字符串,例如:
c = "%s %s"%("Hello",u"World")#c = "Hello" + u"World"
d = u"%s %s"%("Hello","World")#d = u"Hello" + u"World"
4、当使用Unicode字符串时,str()和repr()函数会自动使用默认编码将Unicode字符串转换为标准字符串。对于一个Unicode字符串u,str()相当于u.encode(),repr(u)相当于u"%s"%repr(u.encode('unicode-escape'))
5、而对于format来说,则取决于string字符串类型,format参数将自动转化为与string相同的类型
1. s = "hello"
2. t = u"world"
3. com = "check {}".format(s)
4. print type(com)
5. 总结%格式化,
6. s = "hello"
7. t = u"world"
8. recom = "hello %s"%s
9. print type(recom)
10. 当string为Unicode类型时,%参数无论是什么类型,都将转为Unicode类型。
11. 而当string不是Unicode类型时,格式化之后的字符串类型就是%参数的类型
12.