05-字符串使用基础

python中,单双引号没有区别,表示一样的含义
sentence = 'tom's pet is a cat' # 单引号中间还有单引号,可以转义
sentence2 = "tom's pet is a cat" # 也可以用双引号包含单引号
sentence3 = "tom said:"hello world!""
sentence4 = 'tom said:"hello world"'

三个连续的单引号或双引号,可以保存输入格式,允许输入多行字符串

words = """
hello
world
abcd"""
print(words)

py_str = 'python'
len(py_str) # 取长度
py_str[0] # 第一个字符
'python'[0]
py_str[-1] # 最后一个字符

py_str[6] # 错误,下标超出范围

py_str[2:4] # 切片,起始下标包含,结束下标不包含
py_str[2:] # 从下标为2的字符取到结尾
py_str[:2] # 从开头取到下标是2之前的字符
py_str[:] # 取全部
py_str[::2] # 步长值为2,默认是1
py_str[1::2] # 取出yhn
py_str[::-1] # 步长为负,表示自右向左取

py_str + ' is good' # 简单的拼接到一起
py_str * 3 # 把字符串重复3遍

't' in py_str # True
'th' in py_str # True
'to' in py_str # False
'to' not in py_str # True

sentence = 'tom\'s pet is a cat'
sentence2 = "tom\'s pet is a cat"
sentence3 = "tom said:\"hello world!\""
sentence4 = 'tom said:"hello world"'
print(sentence)
print(sentence2)
print(sentence3)
print(sentence4)
words = """
hello
world
abcd"""
print(words)

py_str = 'python'
len(py_str)
py_str[0]
'python'[0]
py_str[-1]
py_str[2:4]
py_str[2:]
py_str[:2]
py_str[:]
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,338评论 0 10
  • python中,单双引号没有区别,表示一样的含义
    pengwg阅读 514评论 0 0
  • 一.认识字符串 什么是字符串(str)字符串是python中一种常用的有序但是不可变的容器类的数据类型,可以同时存...
    2ez4ddf阅读 3,649评论 0 0
  • 一、python基本数据类型 python基本数据类型有:数字类型Number、字符串类型String、布尔类型B...
    rr1990阅读 3,613评论 0 0
  • 山河万里推开双眼 红尘岁月从画里走来 风暴打翻的人生砚台 浸染过往一圈圈年轮 四季更迭 新旧交替 四面八方奔涌而至...
    空中行云阅读 3,177评论 9 10

友情链接更多精彩内容