Home >Backend Development >Python Tutorial >How to intercept python string

How to intercept python string

angryTom
angryTomOriginal
2020-02-28 12:06:479490browse

This article mainly introduces the method of string slicing (intercepting strings) in Python. The article introduces it in detail through sample code, which has certain reference learning value for everyone's study or work.

How to intercept python string

How to operate python string interception

String index diagram

How to intercept python string

String slicing means intercepting strings and taking substrings.

Recommended: Python video tutorial

String slicing method in Python

String [Start index: End index: step size]

Cut the string from the start index to the end index -1

When the step size is not specified, the step size is 1 String [start index:end index ]

Example: String interception

str = '12345678'
print str[0:1]  # 输出str位置0开始到位置1以前的字符
>> 1
print str[1:6] # 输出str位置1开始到位置6以前的字符
>> 23456
num = 18
str = '0000' + str(num) # 合并字符串
print str[-5:] # 输出字符串右5位
>> 00018

PHP Chinese website, a large number of Introduction to Programming tutorials, welcome to learn.

The above is the detailed content of How to intercept python string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn