字符串介绍

1、字符串在内存中的存储;

2、字符串相加;

3、字符串的格式化;

In [1]: a = 100In [2]: a

Out[2]: 100 #100<255,在堆内存下占用了一个字节,一个字节8位,可以存放的数值最大为255。In [3]: b = "100"In [4]: b

Out[4]: '100' #对应ASCII码存放,一个字节存放任何的一个字符,因此字符串100对应3个字符即占用3个字节。字符串占用空间大。In [5]: type(a)

Out[5]: int

In [6]: type(b)

Out[6]: str

In [4]: b = '100'In [7]: c = "200"In [8]: b +c

Out[8]: '100200'

In [9]: d = "b+c=%s" %(b+c)

In [10]: d

Out[10]: 'b+c=100200'

In [11]: e="abcefg%sgfgfgfg"%b

In [12]: e

Out[12]: 'abcefg100gfgfgfg'

下标索引:就是编号,通过这个编号能找到相应的存储空间。

字符串中下标的使用:字符串实际上就是字符的数组,所以也支持下标索引。

In [27]: name

Out[27]: 'abcdefghijk'In [28]: name[0]

Out[28]: 'a'In [29]: name[-1]

Out[29]: 'k'In [30]: len(name)

Out[30]: 11In [32]: name[len(name)-1]

Out[32]: 'k'In [33]: name[-10]

Out[33]: 'b'In [34]: name[10]

Out[34]: 'k'

切片:是指对操作对象截取其中一部分的操作,字符串、列表、元组都支持切片操作

切片的语法:【起始:结束:步长】

步长是下标变化的规律,默认为1

注意:选取的区间属于左闭右开型(包头不包尾)

In [1]: name="abdfsdfsdf"In [2]: name[1:4]

Out[2]: 'bdf'In [3]: name[:]

Out[3]: 'abdfsdfsdf'In [4]: name[::2]

Out[4]: 'adsfd'In [5]: name[0:]

Out[5]: 'abdfsdfsdf'In [6]: name[:-1]

Out[6]: 'abdfsdfsd'

将输入的字符串倒序输出

1 s=input("请输入一个字符串:")2 i=len(s)3 while i >0 :4 i -= 1

5 print("%s"%(s[i]),end="")6 print("")

[root@localhost python]#python3 20.py

请输入一个字符串:love

evol

[root@localhost python]#

通过切片实现字符串倒序输出

In [1]: name="asdsadsds"In [2]: name

Out[2]: 'asdsadsds'In [3]:name[-1::-1]

Out[3]: 'sdsdasdsa'

字符串操作:函数调用的操作

In [9]: name="love"In [10]: name. //按Tab键显示字符串的方法

name.capitalize name.encode name.format name.isalpha name.islower name.istitle name.lower

name.casefold name.endswith name.format_map name.isdecimal name.isnumeric name.isupper name.lstrip

name.center name.expandtabs name.index name.isdigit name.isprintable name.join name.maketrans>name.count name.find name.isalnum name.isidentifier name.isspace name.ljust name.partition

查找方法:find()和index()

In [10]: my_str="hello world zyj sl everyone in the world"In [11]: my_str.find("zyj")

Out[11]: 12 #目标字符串的第一个字符所在的下标位

In [12]: my_str.find("lw")

Out[12]: -1 #找不到时返回-1,当返回小于0时,即可判断没有找到。

In [13]: my_str.find("world")

Out[13]: 6 #默认从左边开始查找,并输出第一个找到的位置

In [14]: my_str.rfind("world")

Out[14]: 35 #.rfind方法从右边开始查找

In [15]: my_str.index("world")

Out[15]: 6 #index()默认从左边查找

In [17]: my_str.rindex("world")

Out[17]: 35#rindex()从右边查找

In [16]: my_str.index("lw")---------------------------------------------------------------------------ValueError Traceback (most recent call last) in ()----> 1 my_str.index("lw")

ValueError: substringnot found #查找不到时报错,与find()的区别

计数及替换方法:count()和replace()

In [18]: my_str.count("world")#计算目标元素出现的次数。

Out[18]: 2In [19]: my_str.count("lw")

Out[19]: 0

In [20]: my_str.count("zyj")

Out[20]: 1In [21]: my_str.replace("hello","Hi")

Out[21]: 'Hi world zyj sl everyone in the world'In [22]:

In [22]: my_str

Out[22]: 'hello world zyj sl everyone in the world' #原字符串没有改变

In [23]: my_str.replace("world","city")#默认替换所有查找到的目标元素

Out[23]: 'hello city zyj sl everyone in the city'In [24]: my_str.replace("world","city",1) #将查找到的第一个替换,指定count,则替换不超过count次。

Out[24]: 'hello city zyj sl everyone in the world'

分隔:split(),以str为分隔符切片mystr,可以指定最多分割成多少个段。

In [25]: my_str

Out[25]: 'hello world zyj sl everyone in the world'In [26]: my_str.split(" ") #以空格作为分隔符

Out[26]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world']

In [29]: my_str.split() #默认以空格作为分隔符,返回的是列表

Out[29]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world']

In [27]:my_str.split(" ",2) #指定最多包含两个分隔符

Out[27]: ['hello', 'world', 'zyj sl everyone in the world']

In [28]:my_str.split("zyj")

Out[28]: ['hello world', 'sl everyone in the world'] #结果中不显示“zyj”,把它当作分隔符了。

In [30]:my_str.partition("zyj")

Out[30]: ('hello world', 'zyj', 'sl everyone in the world') #将本身也作为一个元素,与split()的区别,包含隔开符。

分隔:partition()注意与split()的区别。

In [25]: my_str

Out[25]: 'hello world zyj sl everyone in the world'In [36]: my_str.partition() #不支持这种写法,必须指定分隔标志。

---------------------------------------------------------------------------TypeError Traceback (most recent call last) in ()----> 1my_str.partition()

TypeError: partition() takes exactly one argument (0 given)

In [37]: my_str.partition(" ")#以空格作为分隔符,遇到的第一个空格作为分隔符,自身也是分隔元素

Out[37]: ('hello', ' ', 'world zyj sl everyone in the world')

In [38]: my_str.partition(' ',3) #不支持设置最大分隔数

---------------------------------------------------------------------------TypeError Traceback (most recent call last) in ()----> 1 my_str.partition(' ',3)

TypeError: partition() takes exactly one argument (2 given)

capitalize():把字符串的第一个字符大写。

title():将每个字符串的第一个单词大写

In [41]: my_str.capitalize()

Out[41]: 'Hello world zyj sl everyone in the world'In [42]: my_str.title()

Out[42]: 'Hello World Zyj Sl Everyone In The World'

startswith():检查字符串以什么开头,返回布尔值:True或False

endswith():检查字符串以什么开头,返回布尔值:True或False

In [43]: my_str.startswith("hello")

Out[43]: True

In [44]: my_str.startswith("Hello")

Out[44]: False

In [45]: my_str.endswith("wor")

Out[45]: False

In [46]: my_str.endswith("world")

Out[46]: True#使用场景

In [50]: file_name="XXX.txt"In [52]: if file_name.endswith(".txt"):

...:print("文本文件")

...:

文本文件

In [53]:

lower():将字符串中的所有大写字符变为小写;

upper():将字符串中的所有小写字母变为大写;

In [57]: my_str

Out[57]: 'Hello WEWER dsfsdf dfsdf'In [58]: my_str.upper()

Out[58]: 'HELLO WEWER DSFSDF DFSDF'In [59]: my_str.lower()

Out[59]: 'hello wewer dsfsdf dfsdf'

#应用场景,验证码不区分大小写,用户输入的进行转换。

In [61]: str="ABC" #目标字符串

In [62]: user_str="Abc" #用户输入字符串

In [63]: user_str.upper() #对用户输入的进行转换后比较

Out[63]: 'ABC'

排列对齐操作:ljust() rjust() center()

In [66]: name

Out[66]: 'ddsfsdfsdfdfdsf'In [67]: name.ljust(50) #50代表长度,返回一个使用空格填充至长度的新字符串

Out[67]: 'ddsfsdfsdfdfdsf'In [68]:

In [68]: name.rjust(50)

Out[68]: 'ddsfsdfsdfdfdsf'In [69]: name.center(50)

Out[69]: 'ddsfsdfsdfdfdsf'In [70]:

删除空白字符:lstrip() rstrip() strip()

In [70]: name="abc"In [71]: name.lstrip() #删除左边的空白符

Out[71]: 'abc'In [72]: name.rstrip() #删除右边的空白符

Out[72]: 'abc'In [73]: name.strip() #删除左右两侧空白符 strip()=trim() java中使用trim()

Out[73]: 'abc'In [74]:

换行符隔开:splitlines()

In [76]: lines="anbc\ndfsdfdf\ndfsdfdf\ndfdf"In [77]: lines.splitlines()

Out[77]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf']

In [78]: lines.split("\n")

Out[78]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf']

In [79]:

字符串中只包含数字、字母、数字或字母、空格的操作,返回布尔值 isalnum() isalpha() isdigit() isspace()

In [1]: test="122323dsfdfsdfsdf"In [2]: test.isalnum()

Out[2]: True

In [3]: test.isalpha()

Out[3]: False

In [4]: test.isdigit()

Out[4]: False

In [5]: test.isspace()

Out[5]: False

In [6]:

In [6]: test1=" "In [7]: test1.isspace()

Out[7]: True

mystr.join(list):mystr中每个字符后面插入list的每个元素后面,构造出一个新的字符串

应用:将列表转换为字符串

In [8]: names=["100","200","300"]

In [9]: names

Out[9]: ['100', '200', '300']

In [10]:"".join(names)

Out[10]: '100200300'In [11]:"-".join(names)

Out[11]: '100-200-300'

查看方法的帮助文档:

In [16]: test="122323dsfdfsdfsdf"

In [14]: help(test.find)

find(...) method of builtins.str instance

S.find(sub[, start[, end]])->int

Return the lowest indexin S where substring sub isfound,

such that subiscontained within S[start:end]. Optional

arguments startand end are interpreted as inslice notation.

Return-1 on failure.

(面试题)给定一个字符串,返回使用空格或者\r \t分割后的倒数第二个字串。

In [1]: name="hel eewrje\twrjwer\twerwer ew\nrewr"In [6]: help(name.split)

In [7]: name.split() #会将字符串中的空格以及特殊意义的符号作为分隔符。

Out[7]: ['hel', 'eewrje', 'wrjwer', 'werwer', 'ew', 'rewr']

In [8]: name.split()[-2] #返回的是列表。列表支持下标操作。

Out[8]: 'ew'

python 输入字符串_python字符串及字符串操作相关推荐

  1. python产生随机字符串_Python生成随机字符串

    原博文 2018-11-02 13:52 − import string import random def get_random_code(length=6, allow_symbol=False) ...

  2. python核心数据类型_Python核心数据类型——字符串

    字符串 Python的字符串是一个有序的字符集合,有序指的是可以通过偏移来访问每个字符,每个字符有严格的从左到右的位置顺序,类似于数组.Python中没有单个字符的类型(C语言中的char),取而代之 ...

  3. python中定义字符串_Python中的字符串String

    Python中除了数字(Numbers)(int,float,complex)之外,另外一种重要的类型就是字符串. 字符串是字符序列,可以由任何字符构成. 在Python语言中,字符串可以放在单引号( ...

  4. python中格式化字符串_Python中所有字符串格式化的指南

    python中格式化字符串 Strings are one of the most essential and used datatypes in programming. It allows the ...

  5. python创建字符串_Python基础之字符串

    1.创建字符串 字符串用于存储和表示文本,在python中属于不可变对象,单引号.双引号.三引号内的内容即字符串. 1 s = '字符串' 2 s1 = "字符串" 3 s2 = ...

  6. python处理多行字符串_python多行字符串

    Python中如何处理长代码格式化问题,如何提高格式化输出的长字符串的可读性? 当我们需要格式化输出一个很长的字符串的时候,都写在一行显得很难看,而且可读性也很差:当我们使用链式的语法写代码的时候常常 ...

  7. python的格式化输入_一看就懂的Python输入和输出、格式化字符串方法

    程序的输出可以有多种形式:我们可以将数据以人类可读的形式打印到屏幕上,或者将其写入到文件中以供后续使用. 格式化输出 迄今为止,在 Python 中存在两种输出值的方法:表达式语句以及 print() ...

  8. Python输入(一维数组、字符串、二维数组、三维数组等)程序

    Python输入的程序归类 输入一个一维数组 #输入一个数组 arr = input() num = [int(n) for n in arr.split(',')] #如果是输入一系列由空格隔开的数 ...

  9. python一个以回车结束的字符串_Python 学习总结——字符串

    1 Python 字符串的 CRUD 操作 1.1 创建字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串,或使用三引号来创建字符串文本段落(允许一个 ...

  10. python中定义字符串_python中的字符串

    python中的字符串一旦定义,则不可以修改 python中的原始字符串 即 在字符串前面加小写字母r 比如:打印输出C:\Program Files\Microsoft Games python中的 ...

最新文章

  1. 拜托,面试别再问我时间复杂度了!!!
  2. Nat. Commun. | 条件GAN网络和基因表达特征用于类苗头化合物的发现
  3. 服务器修改网卡,美国服务器CentOS 6.x修改网卡名称的方法
  4. Java对象的生命周期与作用域的讨论(转)
  5. locks java_java中Locks的使用
  6. SQL日期与时间函数
  7. jQuery特效 | 导航底部横线跟随鼠标缓动
  8. 云南大学软件测试,软件测试大赛云南省省赛在软件学院如期举行
  9. vue下鼠标按住滑动
  10. R3黯然史:从昔日最风光,到如今危机重重
  11. Python代码规范之---代码不规范,亲人两行泪
  12. EXCEL函数LookUp, VLOOKUP,HLOOKUP应用详解(含中文参数解释)
  13. 【Python学习】Python的点滴积累
  14. 如何删除计算机新用户,如何将电脑里的账户信息彻底删除
  15. 白马非马----继承 (转)
  16. office便捷办公05:Mathtype导致office2015复制、粘贴快捷键无法使用问题处理
  17. hosts : IP - 主机名/域名映射
  18. 靖哥哥教你如果拦截去除弹窗广告
  19. [kriging](一)网上下载的kriging克里金的C++程序的初步调试
  20. 读《苔》有感 付强

热门文章

  1. 强引用、弱引用、软引用和虚引用
  2. linux系统安装失败原因,为何我Ubuntu总是安装失败。。。
  3. 面试宝典三 --学科管理模块(拦截器,token,统一异常处理)
  4. i9000正确的刷机方法--odin平台和r…
  5. js/java文字转语音免费(仅仅支持window)支持离线使用
  6. 【 autoware(node:waypoint_extractor)数据保存到多个文件】
  7. vs系列之sln与suo文件
  8. LinearLayout下如何实现两端对齐
  9. Matlab绘制李萨如图(啊啊啊啊啊,好激动,这是我的第一条博客)
  10. IPO前“紧急”分红7500万,上市对赌背后,这家功率半导体公司到底有多“硬”?...