Home >Backend Development >Python Tutorial >How to intercept python string
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 operate python string interception
String index diagram
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!