Python str 函数 - Python零基础入门教程

目录

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门

一.Python str 函数介绍

Pythonstr 即可以表示 字符串 str 类型,也可以作为一个内置函数,可以直接将其他数据类型强制转为字符串类型,语法如下:

'''
参数:
    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]
'''

三.猜你喜欢

  1. Python for 循环
  2. Python 字符串
  3. Python 列表 list
  4. Python 元组 tuple
  5. Python 字典 dict
  6. Python 条件推导式
  7. Python 列表推导式
  8. Python 字典推导式
  9. Python 函数声明和调用
  10. Python 不定长参数 *argc/**kargcs
  11. Python 匿名函数 lambda
  12. Python return 逻辑判断表达式
  13. Python 字符串/列表/元组/字典之间的相互转换
  14. Python 局部变量和全局变量
  15. Python type 函数和 isinstance 函数区别
  16. Python is 和 == 区别
  17. Python 可变数据类型和不可变数据类型
  18. Python 浅拷贝和深拷贝

未经允许不得转载:猿说编程 » Python str 函数

本文由博客 - 猿说编程 猿说编程 发布!

posted @ 2021-07-16 23:24  猿说编程  阅读(2328)  评论(0编辑  收藏  举报