字符串[不可变]

重点方法: in
count()
center(50,'*')
startswith('h')
endwith('h')
find('r') : 找不到返回 -1
index('r'): 找不到会报错
lower()
upper(')
strip()
replace('itle','lesson',1)
split('i',1)

实例:

#  * 重复输出字符串
print("hello\t"*2) # hello hello
# 通过索引获取字符串中字符,这里和列表的切片操作是相同的
print("hello"[2:4]) # ll
# in 成员运算符 - 如果字符串中包含给定的字符返回 True
print('el' in 'hello') # True
print('hello' in ['hello', 'world', '2017']) # True
# % 格式字符串, 字符注意添加引号
print('%s is a China Company' % 'Huawei') # Huawei is a China Company
# + 字符串拼接
print('hello\t'+"world2017") # hello world2017
# join --> + 效率低,该用join,join里面的必须是迭代器,所以必须是列表,元组等’
# 字符串拼接用join 【列表变字符串】
a = 'hello'
b = 'world2018'
# 用******连接a,b
print('******'.join([a, b])) # hello******world2018
print(''.join(('hello\t', 'world2019'))) # hello world2019
print(''.join(['hello\t', 'world2019'])) # hello world2019
print('***'.join(('hello'.strip()+'I love'))) # h***e***l***l***o***I*** ***l***o***v***e
print('******'.join(('hello', 'I love'))) # hello******I love # 字符串的切割,返回一个列表 【字符串变列表】
print('hello world 2017'.split()) # ['hello', 'world', '2017']
print('hello world 2017'.rsplit('l',2)) # ['hel', 'o wor', 'd 2017']
# 字符串的右切割,返回一个列表 【字符串变列表】
print('hello world 2017'.rsplit('l')) # ['he', '', 'o wor', 'd 2017']
# 首字母大写
print('hello'.capitalize()) # Hello
# string.count(str, beg=0, end=len(string))
print('helloworldhelloworld'.count('l', 2, 22)) # 6
# string.decode(encoding='UTF-8', errors='strict') 以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace'
# string.encode(encoding='UTF-8', errors='strict') 以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace' # string.endswith(obj, beg=0, end=len(string))
print('hello'.endswith('o')) # True
# string.startswith(obj, beg=0, end=len(string))
print('hello'.startswith('h')) # True
# string.expandtabs(tabsize=8), 把字符串 string 中的 tab 符号转为空格,默认是8
print('h ello'.expandtabs(tabsize=8)) # h ello
# string.find(str, beg=0, end=len(string)),找不到不报错,返回-1
print('hello'.find('llo')) # 返回2
print('hello'.find('xyz')) # 返回-1,表示查找不到
# 从右开始匹配,返回的字符串原来的位置,真实的索引位置
print('hello'.rfind('l')) # 3
# 查找字符所在的位置, 找不到会报错
print('hello'.index('o')) # 4
# 格式化输出 format
print('hello {name} {year}'.format(name='world', year='2019')) # hello world 2019
# 格式化输出 format_map,要求用字典表示
print('hello {name} {year}'.format_map({'name': 'world', 'year': '{2020}'})) # hello world {2020}
# 判读字符串是否包含数字和字母,至少一个字符 ==> Java里面的正则 \w
print('hello1235'.isalnum()) # True
# 判断是否是个十进制的数字
print('hello2451'.isdecimal()) # False
print('123'.isdecimal()) # True
# 判断是否是一个字母,且至少有一个字符
print("hello".isalpha()) # True
# 判断是否是个数字
print('123'.isdigit()) # True
# 判断是否是个数字
print('9999'.isnumeric()) # True
# 判断非法字符
print('23-hl'.isidentifier()) # True
# 判断字符串是否全是小写
print('Abc'.islower()) # False
# 判断字符串是否全是大写
print('Abc'.isupper()) # False
# 判断是否全是一个空格,只包含空格
print(' hello'.isspace()) # False
# 判断是否首字母大写
print("Hello World".istitle()) # True
print("Hello WOrld".istitle()) # False
# 所有的大写变小写
print("HELLoWorld".lower()) # helloworld
# 所有的小写变大写
print("helloworld".upper()) # HELLOWORLD
# 大写变小写,小写变大写
print('Hello WORLD'.swapcase()) # hELLO world
# 剧中对齐,空格用*填充
print('hello'.center(50, '*'))
# 全部靠左对齐
print("Hello world".ljust(50, '*'))
# 全部靠右对齐
print("Hello world".rjust(50, '*'))
# 去除左右的空格和换行符
print(" Hello World\n ".strip()) # Hello World
# 去除左的空格和换行符
print(" Hello World\n ".lstrip()) # Hello World
# 去除右的空格和换行符
print(" Hello World\n ".rstrip()) # Hello World
# 字符串的替换
print("hello world".replace('llo', 'TTT')) # heTTT world
print("hello world".replace('l', 'Z', 2)) # heZZo world 只替换2次,第三个l忽略
# 按照title的格式输出
print('hello world'.title()) # Hello World

Python学习---Python下[字符串]的学习的更多相关文章

  1. Python复杂场景下字符串处理相关问题与解决技巧

      1.如何拆分含有多种分隔符的字符串¶ ''' 实际案例: 我们要把某个字符串依据分隔符号拆分不同的字段,该字符串包含多种不同的分隔符,例如: s=’ab;cd|efg|hi,jkl|mn\topq ...

  2. 【实测】Python 和 C++ 下字符串查找的速度对比

    完整格式链接:https://blog.imakiseki.cf/2022/03/07/techdev/python-cpp-string-find-perf-test/ 背景 最近在备战一场算法竞赛 ...

  3. 《转》python学习--基础下

    转自http://www.cnblogs.com/BeginMan/archive/2013/04/12/3016323.html 一.数字 在看<Python 核心编程>的时候,我就有点 ...

  4. Python学习---Python下[元组]的学习

    元组是不可变的, 用小括号()定义,而且一旦定义 ,不可变[类型是tuple] [元组看做一个整体,不可拆分,不可赋值,但可以全部重新赋值] 通过圆括号,用逗号分隔,常用在使语句或用户定义的函数能够安 ...

  5. 零基础入门学习Python(14)--字符串:各种奇葩的内置方法

    前言 这节课我们回过头来,再谈一下字符串,或许我们现在再来谈字符串,有些朋友可能觉得没必要了,甚至有些朋友就会觉得,不就是字符串吗,哥闭着眼也能写出来,那其实关于字符串还有很多你不知道的秘密哦.由于字 ...

  6. Python下的OpenCV学习 01 —— 在Linux下安装OpenCV

    一.OpenCV简要介绍 OpenCV是一个跨平台的计算机视觉库,可以运行在Windows.Linux.MacOS等操作系统上.OpenCV提供了众多语言的接口,其中就包含了Python,Python ...

  7. python学习第九讲,python中的数据类型,字符串的使用与介绍

    目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...

  8. “笨方法”学习Python笔记(1)-Windows下的准备

    Python入门书籍 来自于开源中国微信公众号推荐的一篇文章 全民Python时代,豆瓣高级工程师告诉你 Python 怎么学 问:请问你目前最好的入门书是那本?有没有和PHP或者其他语言对比讲Pyt ...

  9. 零基础学习 Python 之字符串

    初识字符串 维基百科对于字符串的定义式:字符串是由零个或者多个字符组成的有限串行.你之前学会敲的第一行 print 代码里的 "Hello World",就是一个字符串.字符串的本 ...

随机推荐

  1. openerp学习笔记 按客户电话、名称模糊查找选择客户(name_search)及客户名称自定义显示(name_get)

    #同时按手机.电话.名称模糊查找选择客户    def name_search(self, cr, user, name, args=None, operator='ilike', context=N ...

  2. (转)heartbeat原理及部署

    原文:http://yjy724.blog.51cto.com/10897133/1840794---------------------------------------------------h ...

  3. 理解restful 架构 && RESTful API设计指南

    restful是前端和后端接口中都会使用的设计思想. 网站即软件,我们也常说的webapp,这种互联网软件采用的是“客户端/服务器”模式,建立在分布式体系上. 网站开发,也可以完全采用软件开发的模式, ...

  4. 使用group by rollup和group by cube后的辅助函数

    本文主要介绍,报表在使用group by rollup和group by cube后的辅助函数. CREATE TABLE TEST8 ( "ID" NUMBER, "O ...

  5. 使用ant宏定义任务

    基础basic.xml文件 <?xml version="1.0" encoding="UTF-8"?><project>    < ...

  6. Python基础(7) - 函数

    Python 函数是一个能完成特定功能的代码块,可在程序中重复使用,减少程序的代码量和提高程序的执行效率.在python中函数定义语法如下: def function_name(arg1,arg2[, ...

  7. Go的接口总结

    一.什么是接口 接口类型是一种抽象的类型,它描述了一系列方法的集合. 接口约定:接口类型中定义的方法即为约定,若一个具体类型实现了所有这些方法,则该类型就满足该接口的约定,或者说它是这个接口类型的实例 ...

  8. 删除trigger 禁用触发器 启用触发器 查看触发器

    删除trigger drop trigger 触发器名 on 在那个表上 禁用触发器 Alter Table 表名 disable trigger 触发器名 启用触发器 Alter Table 表名 ...

  9. JavaScript对象中的this属性

    this属性表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用appl ...

  10. poj 1155 树形背包

    http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...