DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

givenList=[8,4,2,6,7,3,6,9]
oddList=[]
listLength=len(givenList)
for i in range(listLength):
    if(i%2==0):
        oddList.append(givenList[i])
print(oddList)
Enter fullscreen mode Exit fullscreen mode

the above codes can also be written in list comprehension like below

givenList=[8,4,2,6,7,3,6,9]
oddList=[givenList[i] for i in range(len(givenList)) if(i%2==0)]
print(oddList)
Enter fullscreen mode Exit fullscreen mode

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.

givenList=[8,4,2,6,7,3,6,9]
oddList=[::2]
print(oddList)
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)