- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
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)
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : tolearn
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.
- Related Articles
- Extract only characters from given string in Python
- Python Program to Extract Strings with at least given number of characters from other list
- Python – Extract range of Consecutive similar elements ranges from string list
- Program to remove duplicate characters from a given string in Python
- Python program to extract Keywords from a list
- How to extract characters from a string in R?
- Python Program to Extract Elements from a List in a Set
- Java program to delete duplicate characters from a given String
- How to extract first two characters from a string in R?
- Python program to extract ‘k’ bits from a given position?
- Python Program – Strings with all given List characters
- Python program to Reverse a range in list
- Python Program to replace list elements within a range with a given number
- Program to find a good string from a given string in Python
- How to extract initial, last, or middle characters from a string in R?