python 字符 切片,字符串python切片

  python 字符 切片,字符串python切片

  1.字符串下标输出

  模拟方案包含字符串my name="dujunyan "

  获取并输出myname字符串的第一个元素。

  获取myname字符串的第五个元素并输出。

  获取并输出myname字符串的最后一个元素。

  提取myname字符串的倒数第二个元素并输出。

  代码:

  myname= dujunyanPrint(我的名字[0];打印(————边界线——);打印(我的名字[4];打印(————边界线——);print(我的名字[-1];打印(————边界线——);打印(我的名字[-2];打印(————边界线——);结果:

  二。字符串切片操作

  讲解:

  切片就是把要操作的对象的一部分切掉的操作。字符串、列表和元组支持切片操作。

  语法:“开始下标:结束下标3360步”(冒号是英文)))))))65)

  注意:所选间隔从“开始下标”位开始,到“结束下标”位之前的位结束。包括非结束位本身。步骤表示选择间隔。

  下面我以字符串的形式为大家进行操作:

  代码:

  myname= dujunyan#切片操作打印(我的名字[1:4:1];#变量名[start下标:end下标:step]print( ——3354 boundary 3354 );结果:

  以上面字符串为例,进行的操作,取字符串中的“uju”。

  三。字符串中的常见操作

  1.操作方法:查找()。

  描述:检查 python 是否包含my_str字符串,如果包含返回开头的索引值,则返回-1。

  代码:

  my _ str= hello world hello python ;print(my _ str . find) python );# Result 1 (print) my _ str.find)的);#结果2是结果1:

  结果2:

  2.操作方法:index()。

  方法:检查my_str字符串是否包含hello,如果包含,则返回起始索引值。否则,报告一个错误。

  代码:

  my _ str= hello world hello python ;print(my _ str . index( Hello );包含“hello”的结果1显示了下标。如果找到了,就不会继续寻找#print(my_str.index)。# String不包含“s”,结果2中出现错误结果1:

  结果2:

  3.操作方法:计数()。

  描述:返回my_str字符串中“h”在开始(头)和结束之间出现的次数

  代码:

  my _ str= hello world hello python ;print(my _ str . count( h );# Result 1)显示3 #个打印(my _ str . count( s );#如果没有结果2,则返回0结果1:

  结果2:

  4.操作方法:更换()。

  描述:请将my_str字符串中的“hello”替换为“666”,或者如果指定count,则替换为count次或更少。

  代码:

  my _ str= hello world hello python ;print(my_str.replace(Hello , 666 ,2);结果是结果1:

  r;

  5.操作方法:拆分()

  注意:用空格作为分隔符来分割my_str字符串。如果maxsplit有一个指定的值,那么只有maxsplit值字符串会被分隔。

  代码:

  my _ str= hello world hello python ;print(my _ str . split());#结果1显示print(my_str.split(,2));#结果2显示结果1:

  结果2:

  6.操作方法:startswith()

  说明:检查字符串my_str是否以H开头,如果是,则返回True,否则返回False。

  代码:

  my _ str= hello world hello python ;print(my _ str . starts with( h );# Result 1显示true # print (my _ str .以( s )开头);#结果2显示错误结果1:

  结果2:

  7.操作方法:endswith()

  描述:检查字符串my_str是否以N结尾,如果为真,否则为假。

  代码:

  my _ str= hello world hello python ;print(my _ str . ends with( n );# Result 1返回trueprint (3354——分割线33543354 )# print(my _ str . ends with( s );#结果2返回假结果1:

  结果2:

  8.操作方法:上()

  描述:将my_str字符串中的小写字母转换为大写字母。

  代码:

  my _ str= hello world hello python ;print(my _ str . upper());#结果1显示结果1:

  9、操作方法:lower()

  描述:将my_str字符串中的所有大写字符转换为小写。

  代码:

  my _ str= Hello World Hello Python ;print(my _ str . lower());#结果1显示结果1:

  10.操作方法:标题()

  说明:将字符串中每个单词的第一个字母大写。

  代码:

  my _ str= hello world hello python ;print(my _ str . title());#结果1显示结果1:

  1.操作方法:大写()

  说明:将字符串的第一个字符大写。

  代码:

  my _ str= hello world hello python ;print(my _ str . capital());#结果1显示结果1:

  12.操作方法:剥()

  描述:删除my_str字符串两端的空白字符。

  代码:

  my _ str= hello world hello python ;print(my _ str . strip());#结果1结果1:

  13.操作方法:rfind()

  描述:它类似于find()函数,但它是从右边开始的。

  代码:

  my _ str= hello world hello python ;print(my _ str . rfind( world )#结果1结果1:

  14.操作方法:join()

  说明:在每个字符后插入“下划线”连接my_str字符串,构造一个新字符串。

  代码:

  my _ str= _str=[hello , world , hello , python ];print(my _ str . join(str));#结果1显示结果1:

  以上就是Python中的字符串切片以及常见操作

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: