
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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, the characters can be retrieved from 0 to n-1.
For Example, we can index the string "Coding" as 0,1,2,3,4,5, in which the length of the string is 6. 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.
In this article, we will explore all the possible methods to print the characters from a string using Python.
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 -
# 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)
Following is an output of the above example ?
Works Works Works Hey, welcome to TutorialsPoint
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 our requirements, we can directly access our 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.
Following is an example to print characters from a string starting from the 3rd to the 5th by using the indexing and slicing -
# introducing a string String = 'TutorialsPoint' # To get the string starting from thrd to fifth print(String[2:5])
Following is an output of the above program ?
tor
Using Negative Indexing
In Python, Negative Indexing is used to access elements from the end of a list, tuple, or string. While positive indices start from 0, i.e., the beginning of the sequence, whereas negative indices start from -1, which refers to the last element.
Following is an example to print characters from a string starting from the 3rd to the 5th by using a negative index -
String = 'Tutorials' #To get the string starting from thrd to fifth print(String[-7:-4])
Below is the output of the above example ?
tor
Using an 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, then the code in the if block is executed, and if it is false, then the code in the else block is executed.
Following is an example to print characters from a string starting from the 3rd to the 5th by using an 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'))
Here is an output of the above program ?
tor din ogr