Post

PythonPython

K
Kartheeka Nair

Posted on 23rd April 2024|29 views

0
votes

What Are The Examples Of Substring In Python?

Can anyone give me some examples of substring

Answers
P
James ROBERT

Posted on 23rd April 2024

Hi kartheeka,

Let me show you three different examples of a substring in python, but there are several ways you can create substrings in python

Method 1:

A program that Slices string:

m = "abcdefghijklmnopqrs"

for l in range(2, 4):

    print(l, s[l])

    print(l, s[l:l + 2])

    print(l, s[l:l + 3])

    print(l, s[l:l + 4:2])

    print(l, s[l:l + 6:2])

 

Method 2:

A program that gets substring of one-character:

value = "Mindmajix"

for index in range(0, len(value)):

    char = value[index]

print(char)

if char == "M":

        print(Hello)

 

Method 3:

The program which takes substring:

string = "abcdefgh"

male = string[0:2]

Female = string[0:3]

trans = string[0:5]

print(male)

print(female)

print(trans)

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos