Python .format(),%格式化时,对字符串编码方式的影响

1、当标准字符串和Unicode字符串在表达式中混合用时,标准字符串将被自动转换为Unicode字符串。例如:


 
 
  1. s = "hello"
  2. t = u"world"
  3. w = s + t #w = unicode(s) + t
2、当一个字符串方法中使用到Unicode字符串时,结果也总将是Unicode字符串,例如:


 
 
  1. a = "Hello World"
  2. b = a.replace( "World", u"Bob") #b = u"Hello Bob"
此外,使用replace()方法进行零替换(结果仍是原始字符)时,结果仍然会是Unicode字符串。

3、如果一个Unicode字符串使用%操作符做格式字符串,所有参数在一开始都将强制换换位Unicode,然后再根据格式规则连接在一起,如果Unicode对象被用作%操作符的一个参数,整个结果也将是Unicode字符串,例如:


 
 
  1. c = "%s %s"%( "Hello", u"World") #c = "Hello" + u"World"
  2. 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. 1. s = "hello"
  2. 2. t = u"world"
  3. 3. com = "check {}".format(s)
  4. 4. print type(com)
  5. 5. 总结%格式化,
  6. 6. s = "hello"
  7. 7. t = u"world"
  8. 8. recom = "hello %s"%s
  9. 9. print type(recom)
  10. 10. 当string为Unicode类型时,%参数无论是什么类型,都将转为Unicode类型。
  11. 11. 而当string不是Unicode类型时,格式化之后的字符串类型就是%参数的类型
  12. 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="{&quot;mod&quot;:&quot;popu_824&quot;}"><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="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><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="{&quot;mod&quot;:&quot;popu_379&quot;}" 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="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    

转载自:https://blog.csdn.net/wanghuafengc/article/details/21152983

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值