首页 > 编程语言 >python3.10.0字符串基础

python3.10.0字符串基础

时间:2023-01-17 16:58:34浏览次数:52  
标签:python3.10 word character 基础 索引 included 字符串 position

字符串支持 索引 (下标访问),第一个字符的索引是 0。单字符没有专用的类型,就是长度为一的字符串:

  1. >>> word = 'Python'
  2. >>> word[0] # character in position 0
  3. 'P'
  4. >>> word[5] # character in position 5
  5. 'n'

索引还支持负数,用负数索引时,从右边开始计数:

  1. >>> word[-1] # last character
  2. 'n'
  3. >>> word[-2] # second-last character
  4. 'o'
  5. >>> word[-6]
  6. 'P'

注意,-0 和 0 一样,因此,负数索引从 -1 开始。

除了索引,字符串还支持 切片。索引可以提取单个字符,切片 则提取子字符串:


  1. >>> word[0:2] # characters from position 0 (included) to 2 (excluded)
  2. 'Py'
  3. >>> word[2:5] # characters from position 2 (included) to 5 (excluded)
  4. 'tho'
  5. 切片索引的默认值很有用;省略开始索引时,默认值为 0,省略结束索引时,默认为到字符串的结尾:
    
    
    1. >>> word[:2] # character from the beginning to position 2 (excluded)
    2. 'Py'
    3. >>> word[4:] # characters from position 4 (included) to the end
    4. 'on'
    5. >>> word[-2:] # characters from the second-last (included) to the end
    6. 'on'

标签:python3.10,word,character,基础,索引,included,字符串,position
From: https://www.cnblogs.com/yuyuboy/p/17058156.html

相关文章

  • Servlet8 - thymeleaf 基础
    Thymeleaf基础将java内存中的数据加载到在html页面上,称为渲染而Thymeleaf就是一种实现视图渲染的技术添加Thymeleaf的jar包新建一个Servlet类ViewBaseSevlet......
  • GO语言基础语法
    切片与数组typePstruct{ namestring passwordint}funcmain(){ //p:=[3]P{ // {"wqdi",123}, // {"zhang",123123}, // {"qina",123123123}, //......
  • 计算机基础 数据类型 流程控制 字符编码
    目录计算机基础数据类型流程控制字符编码一、关于计算机、编程语言、数据类型、及运算符1.关于计算机2.关于进制数3.关于单位换算4.计算机五大组成部分5.计算机三大核心......
  • AS Android项目的layout文件无法自动补全android:id等基础属性字段
    问题:Android项目的layout文件无法自动补全android:id等基础属性字段<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.co......
  • Python基础
    博客目录python基础部分基础计算机硬件python入门数据类型及常用方法垃圾回收机制用户交互与运算符流程控制数据类型内置方法字符编码文件处理函数函数的基本......
  • Java基础-方法
    Java中分类一共四种类型无参无返回、无参带返回、有参无返回和有参带返回 1.无参无返回值得 publicstaticvoid(){...}2.无参带返回值publicstatic 数据类型()......
  • cshrc基础设置
    每次拿到新机器都要重新设置下shell的rc,让terminal看的更舒服一些,这里把常用的一些语法设置记录下。1aliasclrclear2aliasggvim3aliasls'ls--color'4alias......
  • 使用StringBuilder拼接字符串
    使用StringBuilder拼接字符串/*StringBuilder比String来拼接字符串效率高!@#$需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在......
  • 使用StringBuilder反转字符串
    使用StringBuilder反转字符串importjava.util.Scanner;/*需求:定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果例如,键盘录入abc,输出结......
  • 字符串
    1. 获取字符串长度test='helloworld'1)pythonres=len(test)2)jsres=test.length2. 字符串反转test='helloworld'1)pythonres=test[::-1]2)js......