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.
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/wanghuafengc"> <img src="https://profile.csdnimg.cn/C/5/8/3_wanghuafengc" class="avatar_pic" username="wanghuafengc"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/8.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/wanghuafengc" data-report-click="{"mod":"popu_379"}" target="_blank">语默静喧</a></span> </div> <div class="text"><span>发布了89 篇原创文章</span> · <span>获赞 7</span> · <span>访问量 16万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=wanghuafengc" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>
转载自:https://blog.csdn.net/wanghuafengc/article/details/21152983