Python

How to perform String Slicing in Python?

How to perform String Slicing in Python?

Introduction

You may occasionally need your software to extract a small number of characters from a string. It's known as string slicing.

For example, you can get the area code from a phone number by extracting the first five characters. Alternatively, you might need to take the year portion of a date out of the last four characters.

Using a Step to Slice

By specifying a step through which we can skip characters in the string, Python 3 also enables us to slice a string. We iterate through the string one character at a time because the default step is 1.

After the end index, the step is defined:

string[start:end:step]

Let's check how this functions:

my_string = "This is MY string!"
print(my_string[0:7])  # A step of 1
print(my_string[0:7:2])  # A step of 2
print(my_string[0:7:5])  # A step of 5

OUTPUT :

This is
Ti s
Ti

Reverse Slicing :

A substring that is reversed can also be obtained by cutting a string. In this situation, we would have to reverse the start and finish indices.

Additionally, a harmful action must be offered:

my_string = "This is MY string!"
print(my_string[13:2:-1])
print(my_string[17:0:-2])

OUTPUT:

rts YM si s
!nrsY ish

Slices in Parts:

One thing to remember is that it's not required to define the start and end indices.

The substring will contain all the characters up to the end index if start is not specified.

The substring will start at the start index and continue till  the end if end is not specified.

Let's put that into practise:

my_string = "This is MY string!"
print(my_string[:8]) 
print(my_string[8:]) 
print(my_string[:]) 
print(my_string[::-1])  

OUTPUT:

This is
MY string!
This is MY string!
!gnirts YM si sihT

Getting a certain character at a specific position:

The following is how to extract a character from a particular  position:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
firstCharacter = alphabet[0]
lastCharacter = alphabet[25]

Extraction of a multi-character substring from a string

Python requires that you consider the starting position and the finishing position when slicing a string, and the syntax is as follows:

With this syntax, every character from position start to position end will be extracted.

The substring will begin at position 0 if the start is left empty.

The substring will finish at the string's final character if the end is left empty.

A location (number of characters) from the end of the string is identified by a start value or end value that is negative. The final few characters of a string can be extracted with this.

Slicing the alphabet

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print("The first 5 letters of the alphabet are: " + alphabet[:5])
print("The last 5 letters of the alphabet are: " + alphabet[-5:])
print("The letters in between are: "  + alphabet[5:-5])

Slicing a date

date = "05 January 2019"
day = date[:2]  #First 2 characters
year = date[-4:] #Last 4 characters
month = date[3:-5] #The remaining characters in the middle

Locating if a substring is in a string (and retrieving its position)?

The keyword in makes it simple to determine whether a substring is present in a string.

dateOfBirth = "15 January 2000"
if "January" in  dateOfBirth:
  print("You were born in January!")

Using the find() function, you can also determine exactly where a substring is located within a string:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
pos = alphabet.find("C")
print("Letter C is at position: " + str(pos))

According to this script, the letter "C" is located at position 2. (Despite being the third letter in the alphabet.) This is due to the fact that a string's first letter is always at position 0, never 1!

String Concatenation?

String concatenation is the process of connecting two strings together.

The + sign is used in Python to concatenate strings:

firstname = "James"
print("Hello " + firstname)

write your code here: Coding Playground