如何在Python中对字符串进行子字符串化

Python offers many ways to substring a string. It is often called ‘slicing’.

Python提供了许多对字符串进行子字符串化的方法。 它通常被称为“切片”。

It follows this template:

它遵循以下模板:

string[start: end: step]

Where,

哪里,

start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0.

start :子字符串的起始索引。 该索引处的字符包含在子字符串中。 如果不包括start ,则假定等于0。

end: The terminating index of the substring. The character at this index is NOT included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default.

end :子字符串的终止索引。 该索引处的字符包括在子字符串中。 如果不包括end ,或者指定的值超过字符串的长度,则默认情况下假定它等于字符串的长度。

step: Every ‘step’ character after the current character to be included. The default value is 1. If the step value is omitted, it is assumed to equal to 1.

step :要包括的当前字符之后的每个“ step”字符。 默认值为1。如果省略步长值,则假定等于1。

模板 (Template)

string[start:end]: Get all characters from index start to end-1

string[start:end] :获取从索引startend-1的所有字符

string[:end]: Get all characters from the beginning of the string to end-1

string[:end] :获取从字符串开头到end-1的所有字符

string[start:]: Get all characters from index start to the end of the string

string[start:] :获取从索引开始到字符串结尾的所有字符

string[start:end:step]: Get all characters from start to end-1 discounting every step character

string[start:end:step] :获取从startend-1的所有字符,每字符都打折

例子 (Examples)
  • Get the first 5 characters of a string

    获取字符串的前5个字符

string = "freeCodeCamp"
print(string[0:5])

Output:

输出:

> freeC

Note: print(string[:5]) returns the same result as print(string[0:5])

注意: print(string[:5])返回与print(string[0:5])相同的结果

  • Get a substring of length 4 from the 3rd character of the string

    从字符串的第三个字符获取长度为4的子字符串

string = "freeCodeCamp"
print(string[2:6])

Output:

输出:

> eeCo

Please note that the start or end index may be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (i.e from the right to left). Index -1 represents the last character of the string, -2 represents the second to last character and so on…

请注意,开始或结束索引可能为负数。 负索引意味着您从字符串的末尾开始而不是从头开始计数(即从右到左)。 索引-1代表字符串的最后一个字符,-2代表倒数第二个字符,依此类推……

  • Get the last character of the string

    获取字符串的最后一个字符

string = "freeCodeCamp"
print(string[-1])

Output:

输出:

> p
  • Get the last 5 characters of a string

    获取字符串的最后5个字符

string = "freeCodeCamp"
print(string[-5:])

Output:

输出:

> eCamp
  • Get a substring which contains all characters except the last 4 characters and the 1st character

    获取一个包含最后四个字符和第一个字符以外的所有字符的子字符串

string = "freeCodeCamp"
print(string[1:-4])

Output:

输出:

> reeCode
更多例子 (More examples)
str = “freeCodeCamp”

print str[-5:-2] # prints ‘eCa’
print str[-1:-2] # prints ‘’ (empty string)
  • Get every other character from a string

    从字符串中获取所有其他字符

string = "freeCodeCamp"
print(string[::2])

Output:

输出:

> feCdCm

翻译自: https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值