零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门
一.Python str 函数介绍
在 Python 中 str 即可以表示 字符串 str 类型,也可以作为一个内置函数,可以直接将其他数据类型强制转为字符串类型,语法如下:
'''参数:object — python数据类型对象;返回值: 返回一个str类型的变量;'''str(object)''' 参数: object — python数据类型对象; 返回值: 返回一个str类型的变量; ''' str(object)''' 参数: object — python数据类型对象; 返回值: 返回一个str类型的变量; ''' str(object)
二.Python str 函数使用
# !usr/bin/env python# !usr/bin/env python# -*- coding:utf-8 _*-"""@Author:猿说编程@Blog(个人博客地址): www.codersrc.com@File:Python str 函数.py@Time:2021/04/20 07:37@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]'''# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python str 函数.py @Time:2021/04/20 07:37 @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] '''# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python str 函数.py @Time:2021/04/20 07:37 @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 for循环
- Python 字符串
- Python 列表list
- Python 元组tuple
- Python 字典 dict
- Python 条件推导式
- Python 列表推导式
- Python 字典推导式
- Python 函数声明和调用
- Python 不定长参数 *argc/**kargcs
- Python 匿名函数 lambda
- Python return 逻辑判断表达式
- Python 字符串/列表/元组/字典之间的相互转换
- Python 局部变量和全局变量
- Python type 函数和 isinstance 函数区别
- Python is 和 == 区别
- Python 可变数据类型和不可变数据类型
- Python 浅拷贝和深拷贝
ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容