Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

Is there a way to substring a string in Python, to get a new string from the third character to the end of the string?

Maybe like myString[2:end]?

If leaving the second part means 'till the end', and if you leave the first part, does it start from the start?

1 Answer

0 votes
by (119k points)

We can use slicing to get a sequence of items from the string. Here is the syntax for slicing in python:

sequence[start , stop, step]

This returns from the start element of string to the one element behind the stop index. The step is by default 1 and we can specifiy the value according to the need. 

Here is the example explaining slicing:

myString = 'Python is easy'

print("Substring from the third character to the end of string:", myString[2:] )

print("Substring from the start to the 5th character of string:", myString[:5])

Output:

Substring from the third character to the end of the string: 'thon is easy'

Substring from the start to the 5th character of string: ' Pyth'

You can check out this Python Tutorial to learn in detail about slicing in Python.

Related questions

0 votes
1 answer
0 votes
2 answers
+2 votes
3 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...