Python Tutorial - Slice Function

Python Exercise Lists

A string is composed of multiple characters, and there is an order between the characters. This sequence number is called the index. Python allows you to manipulate single or multiple characters in a string by indexing, such as getting the character at the specified index, and returning the index value of the specified character.

Get a single character in Python

After you know the string name, you can access the corresponding character by using the index in square brackets []. The specific syntax is:


strname represents the string name, and index represents the index value.

Python allows indexing from both ends of a string:

  • When starting from the left end of the string (the beginning of the string), the index is counted from 0; the index of the first character of the string is 0, the index of the second character is 1, and the third string of index is 2.
  • When starting from the right end of the string (the end of the string), the index is counted from -1; the index of the first character of the string is -1 and the index of the second character is -2. Index of third to last character is -3.

For example:

a = ""
print(a[8])  #Get characters with index 8
print(a[-6])  #Get character with index sixth to the last character

The output is:


Get multiple characters (string truncation / string slicing) in Python

In addition to using [] to get a single character, you can also specify a range to get multiple characters, that is, a substring or fragment, the specific format is:

string_id[start : end : step]

Explanation of each part:

  • string_id: string to be intercepted;
  • start: indicates the index of the first character to be truncated (including the character when truncated). If not specified, the default is 0, which is to intercept from the beginning of the string;
  • end: indicates the index of the last character to be intercepted (the character is not included in the interception). If not specified, the default is the length of the string;
  • step: refers to the character indexed by start, one character is obtained every step distance, and the character indexed by end. The default value of step is 1. When this value is omitted, the last colon can also be omitted.

[Example 1] Basic usage:

a = ""
#Get index from 3 at 22 (not including 22) substring
print (a [7: 22])
#Get substring with index from 7 to -6
print (a [7: -6])
#Starting at index 3, take one character every 4 characters until index 22
print (url [3: 22: 4])

The output is:


[Example 2] Advanced usage, start, end, step three parameters can be omitted:

a = ""
#Get the substring from index 6 to the end
print (a [6:])
#Get the substring starting at index-13 and ending at the end
print (a [-13:])
#Truncate the string from the beginning until index 18
print (a [: 18])
#Take one character every 5 characters
print (a [:: 5])

The output is:


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises