下标

name = 'abcdef'

print(name[0])
print(name[1])
print(name[2]) # a
# b
# c

切片

# 切片的语法:[起始:结束:步长]
name = 'abcdef' print(name[0:3])
# abc print(name[2:])
# cdef print(name[1:-1])
# bcde print(name[::-2])
# fdb print(name[1:5:2])
# bd print(name[5:1:-2])
# fd print(name[::-1])
# fedcba

字符串操作

mystr = 'hello world wxhwcm and wiozxcpp'

# 检测 world 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
print(mystr.find("world"))
print(mystr.find("world", 0, len(mystr)))
#
# # 跟find()方法一样,只不过如果str不在 mystr中会报一个异常
print(mystr.index("world", 0, len(mystr)))
# # 返回 str在start和end之间 在 mystr里面出现的次数
print(mystr.count("o", 0, len(mystr)))
# # 把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.
print(mystr.replace("world", "zhangsan", mystr.count("world")))
# hello zhangsan itcast and itcastcpp # 以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
print(mystr.split(" ", 2))
# ['hello', 'world', 'itcast and itcastcpp'] # 把字符串的第一个字符大写
print(mystr.capitalize())
# Hello world itcast and itcastcpp # 把字符串的每个单词首字母大写
print(mystr.title())
# Hello World Itcast And Itcastcpp # 检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False
print(mystr.startswith("hello"))
# True # 检查字符串是否以obj结束,如果是返回True,否则返回 False
print(mystr.endswith("cpp"))
# True # 转换 mystr 中所有大写字符为小写
mystr.lower() # 转换 mystr 中所有小写字母为大写
mystr.upper() # 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
mystr.ljust(40) # 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
mystr.rjust(40) # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
mystr.center(40) # 删除 mystr 左边的空白字符
mystr.lstrip() # 删除 mystr 字符串末尾的空白字符
mystr.rstrip() # 删除 mystr 字符串两端的空白字符
mystr.strip() # 类似于 find()函数,不过是从右边开始查找
mystr.rfind("xh", 0, len(mystr)) # 类似于 index(),不过是从右边开始
mystr.rindex("o", 0, len(mystr)) # 把mystr以str分割成三部分,str前,str和str后
print(mystr.partition("wxhwcm"))
# ('hello world ', 'wxhwcm', ' and wiozxcpp') # 类似于 partition()函数,不过是从右边开始.
print(mystr.rpartition("c"))
# ('hello world wxhwcm and wiozx', 'c', 'pp') # 按照行分隔,返回一个包含各行作为元素的列表
mystr.splitlines() # 如果 mystr 所有字符都是字母 则返回 True,否则返回 False
mystr.isalpha() # 如果 mystr 只包含数字则返回 True 否则返回 False
mystr.isdigit() # 如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False
mystr.isalnum() # 如果 mystr 中只包含空格,则返回 True,否则返回 False
mystr.isspace() # mystr 中每个字符后面插入str,构造出一个新的字符串
print("-o-".join("hive"))
# h-o-i-o-v-o-e

3、Python-字符串的更多相关文章

  1. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  4. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  5. python字符串基础知识

    1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...

  6. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

  7. Python 字符串操作

    Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...

  8. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

  9. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  10. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

随机推荐

  1. 【Alpha阶段】展示博客发布!

    1.团队成员简介 Email:qianlxc@126.com Free time:8:00 7:00 a.m ~ 11:00 12:00p.m Introduction: 我是一个热情的人.开朗的人. ...

  2. 我的集合学习笔记--LinkedList

    一,Node节点: /** * 存储元素基本单位 */ public class Node { Object data; Node pre; Node next; public Node(Node p ...

  3. PAT 1038 统计同成绩学生

    https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040 本题要求读入N名学生的成绩,将获得某一给定分 ...

  4. Java日志输出问题

    以前有一个同事,说自己的Java控制台程序,输出的信息,打印信息以及错误信息,在windows的command line刷屏,想复制下来,想要自输出到日志文件里. 自己写文件太麻烦,他从网上只找到用重 ...

  5. 安裝CentOS7后修復win7引导

    想尝试双系统的心情想必大家都能理解,但是安装了双系统之后的收尾工作也是必不可少的,由于对Linux并不算很熟悉,所以在这方面花了不少时间,这里将CentOS7下修復windows7引导的解决方案记录下 ...

  6. Mysql 5.7.21 设置主从库同步

    主从复制条件: Mysql 单机多实例安装参考Mysql 5.7.21 设置主从库同步 下面的操作是多实例主从复制,3306为主库,3307为从库. 主库要开启log-bin,主库和从库的server ...

  7. MSTSC 3389 端口修改

    1. 启动注册表编辑器. 2. 找到并单击以下注册表子项: 3. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server ...

  8. DOM中表格的操作方法总结

    DOM中表格的操作方法总结 <table/>元素的方法如下: caption:指向<caption/>元素(如果存在): tBodies:<tbody/>元素的集合 ...

  9. 快速排序Qsort

    快速排序Qsort是所有学习算法和数据结构最基础的一个部分,也是考试题和面试的一个小重点. 快速排序的时间复杂度为O(N*lgN),而且常数因子很小. 对于随机数据,效率特别高: 对于构造的恶意数据, ...

  10. CF1037D Valid BFS?

    Valid BFS? CodeForces - 1037D The BFS algorithm is defined as follows. Consider an undirected graph ...