
- 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 – Extract Strings with Successive Alphabets in Alphabetical Order
When it is required to extract strings which have successive alphabets in alphabetical order, a simple iteration, and the ‘ord’ method for Unicode representation is used.
Example
Below is a demonstration of the same −
my_list = ["python", 'is', 'cool', 'hi', 'Will', 'How'] print("The list is :") print(my_list) my_result = [] for element in my_list: for index in range(len(element) - 1): if ord(element[index]) == ord(element[index + 1]) - 1: my_result.append(element) break print("The result is :") print(my_result)
Output
The list is : ['python', 'is', 'cool', 'hi', 'Will', 'How'] The result is : ['hi']
Explanation
A list of strings is defined and is displayed on the console.
An empty list is defined.
The list is iterated over, and the Unicode character of consecutive elements in the list is compared.
If they are equal, it is appended to the empty list.
The control breaks out of the loop.
This list is displayed as the output on the console.
- Related Articles
- Python code to print common characters of two Strings in alphabetical order
- Print common characters of two Strings in alphabetical order in C++
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Python – Extract rows with Even length strings
- Java code to print common characters of two Strings in alphabetical order
- Python – Extract Sorted Strings
- Python Program to Extract Strings with a digit
- Sort the array of strings according to alphabetical order defined by another string in C++
- C program to sort names in alphabetical order with string functions.
- Extract numbers from list of strings in Python
- How to return a passed string with letters in alphabetical order in JavaScript?
- Python – Concatenate Strings in the Given Order
- C program to sort names in alphabetical order
- Check if the characters of a given string are in alphabetical order in Python
- Get table column names in alphabetical order in MySQL?

Advertisements