python slicing string

Python String Slicing

Sometimes you may need to split a string into array of characters, or a set of substrings, based on different conditions. In this article, we will learn different ways to slice python string. There are two ways to slice string in python – using slice() constructor and using indexing.


Python String Slicing

Here are the couple of ways to slice python string.


1. Using slice()

Here is the syntax of slice() function.

Syntax:

slice(stop)
slice(start, stop, step)

Parameters:
start: Starting index where the slicing of object starts.
stop: Ending index where the slicing of object stops.
step: It is an optional argument that determines the increment between each index for slicing.

Return Type: Returns a sliced object containing elements in the given range only.

Here is an example to use slice(). In slice() function if you specify only one argument then slice() function will take it as the stop index for slicing and extract characters till that index. If you specify 2 values, they will be taken as start & stop indexes. If you specify 3 values, it will be taken as start, stop and step.

# Python program to demonstrate
# string slicing

# String slicing
String ='ASTRING'

# Using slice constructor
s1 = slice(3)
s2 = slice(1, 5)
s3 = slice(-1, -12, -2)

print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])

Here is the output you will see.

String slicing
AST
STRIN
GITA

In the first example, it selects the first 3 characters. In the next example, it selects all characters from index position 1 to 5. In the last example, it selects all characters from last 1 to last 12 indexes, skipping 2 characters at a time.

If any of the indexes are more than the length of your string, it will wrap around the string and start counting the index again. For example, in the above example, if start position is given as 7, then slice() function will count till letter G which has index 6, and then start from letter A as index 7.


2. Using Indexes

In python, each string is seen as an array of characters. So you can also use indexes to extract characters and substrings from a given string. You can also directly use character indexes to directly extract one or more characters or substring from a given string in python.

# Python program to demonstrate
# string slicing

# String slicing
String ='ASTRING'

# Using indexing sequence
print(String[:3])
print(String[1:5])
print(String[-1:-12:-2])

# Prints string in reverse
print("\nReverse String")
print(String[::-1])

In the above example, we define a string ‘ASTRING’ and use string indexes in 3 ways to extract characters from our string. In the first example, we extract first 3 characters. In the second example, we extract all characters starting from index position 1 to index position 5. In the 3rd example, we extract all characters from last character to last but 12th character, skipping 2 characters at a time.

In the last example, we print the given string in reverse. In this case, you will see the following output.

AST
SR
GITA

Reverse String
GNIRTSA

In this article, we have learnt how to slice python string using slice() function and using string index.

Also read:

How to Select Rows from Dataframe Using Column Values
How to Rename Columns in Pandas
How to Create Pandas Dataframe from Dictionary
How to Create Pandas Dataframe from Lists
How to Access Index of Last Element in Pandas Dataframe

Leave a Reply

Your email address will not be published.