


Extraction of Element of a string at odd,even or any pattern position in Python
Extracting Element in odd position of a String
Here I will show you simple way to extract all element in Odd position of a String.
Example of what we are trying to do here look like below
As you can see the nature of this problem we can easily solve this by using any one of the following method.
Method1 (using for loop)
since we need to be selecting any element in odd position this means we need to extract element in index 0,2,4,6,8.... do not forget that this is equaivalent to 1,3,5,7,9.... that everyone will called odd number since python usually start counting from zero. Now type the following codes
Codes>>
givenString='this is our string' oddString='' stringLength=len(givenString) for i in range(stringLength): if(i%2==0): oddString+=givenString[i] print(oddString)
the codes above loop through the given string, check if the element index is even if even then add the element to the odd String. Don't be confuse because we are testing for even in the codes. this is because of the way python works.
MethodII (custom method)
This method is normal method of extracting element in a string or iterable object like list. it is very common although many people don't realize deep usage of it. take a look at the following codes
Codes>>
givenString='this is our string' oddString=givenString[0::2] print(oddString)
The codes above is very short and simple. It starts picking element from index 0 then pick the next element by increasing the index by 2.
in another word, 0 written in the above codes specify the start and the index of the first element to be selected : denoted to failure to put number after the first column means to the last index. the seconde column and 2 means with the gap of two.
The same code could be written like below
Codes>>
givenString='this is our string' oddString=givenString[::2] print(oddString)
Here we don't specify the start, which means it should start extracting from the first element which is the same thing as typing 0.
Extracting Element in Even position of a String
Now that you have understand how to extract element in odd position of a string to extract element in even position will not be difficult. All you just have to do is to make a slight change. take a look at the example below using first method.
Codes>>
givenString='this is our string' oddString='' stringLength=len(givenString) for i in range(stringLength): if(i%2==1): evenString+=givenString[i] print(oddString)
the codes above loop through the given string, check if the element index is odd if odd then add the element to the even String. remember what is odd number for code is even number for human.
Using the second Method we just need to do little adjustment to where we will start the extraction.
Codes>>
givenString='this is our string' oddString=givenString[1::2] print(oddString)
I hope you understand how the examples above worked?? if there is anything you don't understand don't hesitate to message me.
Extracting Element in odd position and even position of a List or array
You might want to extract element in odd position of a list or array too. to do that is exactly the same way we did for the string take aloook at the example below
Codes
givenString='this is our string' oddString='' stringLength=len(givenString) for i in range(stringLength): if(i%2==0): oddString+=givenString[i] print(oddString)
the above codes can also be written in list comprehension like below
givenString='this is our string' oddString=givenString[0::2] print(oddString)
both codes above works well and you can easily find the even part by changing the if condition from 0 to 1.
can we use custom method for this too?? ofcourse you can use it infact that is the recomended method. see the example below.
givenString='this is our string' oddString=givenString[::2] print(oddString)
I hope you learn something from this article??
if you have any question don't hesitate to ask. chat me up on WhatsApp or Mail. Don't forget to follow me on Twitter so that you don't miss any of my articles.
The above is the detailed content of Extraction of Element of a string at odd,even or any pattern position in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
