一.str函數介紹
在python中str即可以表示 字符串str 類型,也可以作為一個內置函數,可以直接將其他數據類型強制轉為字符串類型,語法如下:
str(object)
參數:
object — python數據類型對象;
返回值: 返回一個str類型的變量;
二.str函數使用
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個人博客地址): shuopython.com @WeChat Official Account(微信公眾號):猿說python @Github:www.github.com @File:python_str.py @Time:2019/12/11 21:25 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累! """ # 案例1: 將整形或者浮點數類型轉為str字符串類型 print(type(str(3.5))) print(str(3.5)) print("***"*20) # 案例2: 將字典轉為str字符串類型 dict1 = {'www': 'shuopython.com', 'google': 'google.com'} print(type(str(dict1))) print(str(dict1)) print("***"*20) # 案例3: 將列表轉為str字符串類型 list1 = [1, 2, 3, 4] print(type(str(list1))) print(str(list1))
輸出結果:
<class 'str'> 3.5 ************************************************************ <class 'str'> {'www': 'shuopython.com', 'google': 'google.com'} ************************************************************ <class 'str'> [1, 2, 3, 4]
猜你喜歡:
轉載請注明:猿說Python » python str函數
技術交流、商務合作請直接聯系博主
掃碼或搜索:猿說python

猿說python
微信公眾號 掃一掃關注