How to print characters from a string starting from 3rd to 5th in Python?



Python uses arrays of bytes called strings to represent unicode characters. In Python, string indexing ranges from 0 to n-1, where n is the length of the string. In a string of size n, characters can therefore be retrieved from 0 to n-1.

For instance, the index of the text “Coding” is 0,1,2,3,4,5. The first character in the string “Coding” is represented by the number 0, and the characters o, d, i, n, and g are represented by the numbers 1, 2, 3, and 5, respectively.

Printing characters of a string

Characters can be put inside single or double quotations to make strings. In Python, even triple quotes are permissible, but they are often only used to denote multiline strings and docstrings. Following is an example to create a string −

Example

# Defining the string in single quote string = 'Works' print(string) # In double quotes string = "Works" print(string) # In triple quotation string = '''Works''' print(string) # Extensing lines in triple quotation string string = """Hey, welcome to TutorialsPoint""" print(string)

Output

Following is an output of the above code −

Works
Works
Works
Hey, welcome to
TutorialsPoint

The methods for retrieving the characters from a string by indexes are listed below.

Printing 3rd to 5th characters of a String

To print characters of a string starting from the index 3 to 5, there are various ways, they are as follows −

Using Indexing or slicing

A character's position in the string is indicated by its index. In Python, indexing is a technique used to refer to specific elements within an iterable by their position. In other words, depending on your requirements, you can directly access your preferred elements within an iterable and perform different operations.

Syntax

Following is the syntax to print the characters from a string −

string[start:end:step]

Where,

  • Start − The substring's initial index. The substring contains the character at this index. Start is taken to be equal to zero if it is left out.
  • End − The substring's finishing index. This character is not a part of the substring at this index. End is automatically considered to be equal to the length of the string if it is omitted, or if the provided value is longer than the string.
  • After the current character, each "step" character must be provided. The initial setting is 1. Step is taken to be equal to 1 if it is left out.

Example

Following is an example to print characters from a string starting from 3rd to 5th by indexing −

# introducing a string String = 'TutorialsPoint' # To get the string starting from thrd to fifth print(String[2:5])

Output

Following is an output of the above code −

tor

Using Negative Indexing

We can use negative integers if we just care about the last few elements of a list or if we simply want to index the list starting at the other end. Negative indexing is the name given to this method of indexing from the opposite end.

Example

The last element is represented by -1 in negative indexing rather than -0.

Following is an example to print characters from a string starting from 3rd to 5th by using negative index −

String = 'Tutorials' #To get the string starting from thrd to fifth print(String[-7:-4])

Output

Following is an output of the above code 

tor

Using if-else condition

The true and false parts of a given condition are both executed using the if-else statement. If the condition is true, the code in the if block is performed; if it is false, the code in the else block is executed.

Example

Following is an example to print characters from a string starting from 3rd to 5th by using if-else condition −

def third_to_fifth(str): return str[2:5] if len(str) > 3 else str print(third_to_fifth('Tutorials')) print(third_to_fifth('Coding')) print(third_to_fifth('Program'))

Output

Following is an output of the above code 

tor
din
ogr

Advertisements