

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python – Concatenate Strings in the Given Order
When it is required to concatenate strings in a given order, a simple iteration is used.
Example
Below is a demonstration of the same −
my_list = ["pyt", "fun", "for", "learning"] print("The list is :") print(my_list) sort_order = [1, 0, 3, 2] my_result = '' for element in sort_order: my_result += my_list[element] print("The result is :") print(my_result)
Output
The list is : ['pyt', 'fun', 'for', 'learning'] The result is : funpytlearningfor
Explanation
A list is defined and displayed on the console.
Another list of integers is defined, which is the order of sorting.
An empty string is created.
The sort order list is iterated over, and the element being iterated over is used as index to access elements from the string list.
This is appended to the empty string.
This is the output that is displayed on the console.
- Related Questions & Answers
- C++ program to concatenate strings in reverse order
- How to concatenate two strings in Python?
- Concatenate strings in Arduino
- Python program to concatenate Strings around K
- Concatenate Multiple Strings in Java.
- Concatenate 2 strings in ABAP without using CONCATENATE function
- The Optimum Method to Concatenate Strings in Java.
- Can MySQL concatenate strings with ||?
- How to concatenate strings in R?
- C++ Program to Concatenate Two Strings
- Find a string in lexicographic order which is in between given two strings in Python
- How to concatenate two strings in C#?
- How to concatenate several strings in JavaScript?
- Different ways to concatenate Strings in Java
- How to concatenate two strings in Golang?
Advertisements