字符串[不可变]

重点方法: 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. (转)mysql 5.6 原生Online DDL解析

    做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DROP,ALTER等)代价是非常高的,特别是在单表上千万的情况下,加个索引或改个列类型,就有可能堵塞整个表的读写. 然后 mysq ...

  2. Oracle PL/SQL编程之变量

    注: 以下测试案例所用的表均来自与scott方案,使用前,请确保该用户解锁. 1.简介 和大多数编程语言一样,在编写PL/SQL程序时,可以定义常量和变量,在pl/sql程序中包括有: a.标量类型( ...

  3. JavaEE_XMind总结

    1 Servlet 2 ServletContext 3 HttpServletResponse 4 HttpServletResquest 5 Cookie

  4. poj 3750 小孩报数问题

    小孩报数问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11527   Accepted: 5293 Descripti ...

  5. Java中break、continue及标签等跳转语句的使用[上]

    java 中跳转语句使用break.continue和标签,各自或组合完成相应的功能. 今天做题时遇到关于标签命名规范,顺便将跳转语句语法都看了一遍,很有收获. 在<Java编程思想>一书 ...

  6. [PY3]——内置数据结构(8)——解构与封装

    ### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...

  7. Go语言备忘录(1):基本数据结构

    本文内容是本人对Go语言的变量.常量.数组.切片.映射.结构体的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处:Go语言备忘录(1):基本数据 ...

  8. java中list、set、map区别(转)

    Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap Collecti ...

  9. 如何理解animation-fill-mode及其使用?<转>

    今天看了css3的动画,对animation的其他属性都比较容易理解,唯独这个animation-fill-mode让我操碎了心. 找了些下面的描述: 规定对象动画时间之外的状态. 有四个值可选,并且 ...

  10. 快速删除node_modules目录

    当node项目需要重新安装依赖,并且需要删除原有的node_modules目录时,windows下删除该目录比较麻烦的,所以我就在网上找了个npm包,名字叫做 rimraf 安装步骤: npm ins ...