 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Python program to extract characters in given range from a string list
When it is required to extract characters in given range from a string list, a list comprehension and list slicing is used.
Example
Below is a demonstration of the same −
my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result)  Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : tolearn
Explanation
- A list is defined and displayed on the console. 
- The value for ‘start’ and ‘end’ are defined. 
- A list comprehension is used to iterate over the list, and every element between the ‘start’ and ‘end’ value is extracted, and then the 'join' method is used to remove the spaces. 
- This result is assigned to a variable. 
- This is the output that is displayed on the console. 
Advertisements
 