Python str()鍑芥暟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python str()鍑芥暟相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%bd%a2%e5%bc%8f' title='褰㈠紡'>褰㈠紡 鍙傛暟 鍗忚 details dash 璋冪敤 瀛楀吀 article
鎻忚堪
str鍑芥暟鏄疨ython鐨勫唴缃嚱鏁帮紝瀹冨皢鍙傛暟杞崲鎴愬瓧绗︿覆绫诲瀷锛屽嵆浜洪€傚悎闃呰鐨勫舰寮忋€?/p>
璇硶
str(object)
鍚嶇О 璇存槑 澶囨敞
object 寰呰杞崲鎴愬瓧绗︿覆鐨勫弬鏁?鍙渷鐣ョ殑鍙傛暟
杩斿洖鍊硷細杩斿洖object鐨勫瓧绗︿覆褰㈠紡
浣跨敤绀轰緥
1. 鏃犲弬璋冪敤
褰搒tr()鍑芥暟鐨勫弬鏁伴兘鐪佺暐鏃讹紝鍑芥暟杩斿洖绌哄瓧绗︿覆銆傝繖绉嶆儏鍐靛父鐢ㄦ潵鍒涘缓绌哄瓧绗︿覆鎴栬€呭垵濮嬪寲瀛楃涓插彉閲忋€?/p>
>>> str()
鈥樷€?br />2. 涓嶇渷鐣ュ弬鏁?br />str鍑芥暟灏嗘暣鏁般€佹诞鐐规暟銆佸垪琛ㄣ€佸厓缁勩€佸瓧鍏稿拰闆嗗悎杞崲涓哄瓧绗︿覆绫诲瀷
>>> str(-23) #鏁存暟杞崲涓哄瓧绗︿覆
鈥?23鈥?br />
>>> str(1.3e2) #娴偣鏁拌浆鎹负瀛楃涓?br />鈥?30.0鈥?br />
>>> a_list = [12, 鈥?23.1鈥? 鈥楶ython鈥榏
>>> str(a_list) #鍒楄〃杞崲涓哄瓧绗︿覆
"[12, 鈥?23.1鈥? 鈥楶ython鈥榏"
>>> str(a_list)[0]
鈥榌鈥?br />
>>> a_tuple = (23, 鈥?we鈥? -8.5)
>>> str(a_tuple) #鍏冪粍杞崲涓哄瓧绗︿覆
"(23, 鈥?we鈥? -8.5)"
>>> a_dictionary = {鈥楬uawei鈥?鈥楥hina鈥? 鈥楢pple鈥?鈥楿SA鈥榼
>>> str(a_dictionary) #瀛楀吀杞崲涓哄瓧绗︿覆
"{鈥楬uawei鈥? 鈥楥hina鈥? 鈥楢pple鈥? 鈥楿SA鈥榼"
>>> str(a_dictionary)[10]
鈥?鈥?br />
>>> a_set = {鈥楥hina鈥? 鈥楯apan鈥? 鈥楿K鈥榼
>>> str(a_set) #闆嗗悎杞崲涓哄瓧绗︿覆
"{鈥楯apan鈥? 鈥楿K鈥? 鈥楥hina鈥榼"
??娉ㄦ剰锛氬皢鍒楄〃銆佸厓缁勩€佸瓧鍏稿拰闆嗗悎杞崲涓哄瓧绗︿覆鍚庯紝鍖呰9鍒楄〃銆佸厓缁勩€佸瓧鍏稿拰闆嗗悎鐨勨€榌鈥樸€佲€榏鈥樸€佲€?鈥樸€佲€?鈥樸€佲€榹鈥樸€佲€榼鈥橈紝浠ュ強鍒楄〃銆佸厓缁勩€佸瓧鍏稿拰闆嗗悎涓殑鍏冪礌鍒嗛殧绗︹€?鈥橈紝鍜屽瓧鍏镐腑閿€煎鈥?鈥樹篃閮借浆鎹㈡垚浜嗗瓧绗︿覆锛屾槸杞崲鍚庡瓧绗︿覆鐨勪竴閮ㄥ垎銆?/p>
娉ㄦ剰浜嬮」
str()鍑芥暟鍙互灏嗗瓧绗︿覆绫诲瀷杞崲涓哄瓧绗︿覆锛岃繖涓€杩囩▼涓嶄細鏈変换浣曟姤閿欙紝浣嗘槸浼氫骇鐢熼澶栫殑鏃剁┖娑堣€椼€?/p>
>>> str(鈥榓sdf鈥?
鈥榓sdf鈥?br />
鏈川鎰忎箟
————————————————
鐗堟潈澹版槑锛氭湰鏂囦负CSDN鍗氫富銆孴CatTime銆嶇殑鍘熷垱鏂囩珷锛岄伒寰?CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍙婃湰澹版槑銆?br />鍘熸枃閾炬帴锛歨ttps://blog.csdn.net/TCatTime/article/details/82963437
以上是关于Python str()鍑芥暟的主要内容,如果未能解决你的问题,请参考以下文章