Today, I practised different Python problems using strings, lists, loops, conditions, and list comprehensions.

1. Separate Alphabets and Numbers from a String

Problem:

Given a string containing alphabets and numbers, separate them and display the alphabets first, followed by the numbers.

Input:

s = 'a1b2c3'

Expected Output:

abc123

Using a for loop:

s = 'a1b2c3' alpha = '' numbers = '' for letter in s: if letter.isalpha(): alpha = alpha + letter else: numbers = numbers + letter print(alpha + numbers) 
Output: abc123 

Understanding the logic:

  • alpha stores all alphabetic characters.
  • numbers stores all numeric characters.
  • isalpha() checks whether a character is an alphabet.
  • isdigit() can be used to check whether a character is a digit.
  • Finally, we combine both strings.

Using List Comprehension:

We can solve the same problem using list comprehension.

s = 'a1b2c3' l = [letter for letter in s if letter.isalpha()] d = [letter for letter in s if letter.isdigit()] output = ''.join(l + d) print(output) 
 Output: abc123 

Here:

  • The first list comprehension collects alphabets.
  • The second list comprehension collects digits.
  • l + d combines both lists.
  • join() converts the combined list into a string.

2. Remove Duplicate Characters from a String

Problem:

Remove repeated characters from a string while maintaining the original order of characters.

Input:

first = 'aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbccccccccccddddddddddddddd'

Expected Output:

abcd

Using a for loop:

 first = 'aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbccccccccccddddddddddddddd' second = '' for letter in first: if letter not in second: second = second + letter print(second) 
 Output: abcd 

Understanding the logic:

  • Initially, second is an empty string.
  • The loop takes each character from first.
  • if letter not in second checks whether the character is already available.
  • If it is not available, we add it to second.
  • If it is already present, we skip it. This way, only the first occurrence of each character is stored.

3. Convert Alternate List Elements into Lowercase

Problem:

Convert the elements at odd indexes into lowercase while keeping the elements at even indexes unchanged.

Input:

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO']

Expected Output:

['ABC', 'def', 'GHI', 'jkl', 'MNO']

Using a for loop:

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'] for letter in range(len(l)): if letter % 2 != 0: l[letter] = l[letter].lower() print(l) 
Output: ['ABC', 'def', 'GHI', 'jkl', 'MNO'] 

Understanding the logic:

  • range(len(l)) generates the indexes of the list.
  • letter % 2 != 0 checks whether the index is odd.
  • lower() converts the string into lowercase.
  • We update the original list using its index.

Using enumerate():

The enumerate() function gives us both the index and the element while iterating through a list.

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'] for index, element in enumerate(l): if index % 2 == 0: print(element, end=' ') else: print(element.lower(), end=' ') 
 Output: ABC def GHI jkl MNO 

Using a while loop:

 l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'] i = 0 while i < len(l): if i % 2 == 0: print(l[i], end=' ') else: print(l[i].lower(), end=' ') i += 1 
 Output: ABC def GHI jkl MNO 

4. Reverse Alternate List Elements

Problem:

Reverse the strings at odd indexes and keep the strings at even indexes unchanged.

Input:

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO']

Expected Output:

['ABC', 'FED', 'GHI', 'LKJ', 'MNO']

Using a for loop:

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'] for letter in range(len(l)): if letter % 2 != 0: l[letter] = l[letter][::-1] print(l) 
Output: ['ABC', 'FED', 'GHI', 'LKJ', 'MNO'] 

Understanding the logic:

  • letter % 2 != 0 identifies odd indexes.
  • [::-1] reverses a string.
  • We assign the reversed string back to the original list position.

Using List Comprehension and enumerate():

l = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'] output = [ element if index % 2 == 0 else element[::-1] for index, element in enumerate(l) ] print(output) 
 Output: ['ABC', 'FED', 'GHI', 'LKJ', 'MNO'] 

Here, the conditional expression checks the index:

  • Even index: keep the original element.
  • Odd index: reverse the element.

5. Repeat Characters Based on Their Following Numbers

Problem:

Consider a string where every alphabet is followed by a number. Repeat each alphabet according to the number that follows it.

Input:

s = "a1b2c3"

Expected Output:

abbccc

Solution Using a while Loop

s = "a1b2c3" result = "" i = 0 while i < len(s): letter = s[i] number = int(s[i + 1]) result += letter * number i += 2 print(result) 
 Output: abbccc 

Understanding the logic:

  • i = 0 starts from the first character.
  • letter = s[i] gets the alphabet.
  • number = int(s[i + 1]) gets the number immediately after the alphabet.
  • letter * number repeats the alphabet.
  • result += adds the repeated characters to the result.
  • i += 2 moves to the next alphabet-number pair.

Important: Python allows string repetition using the * operator.

For example:

 print('a' * 1) print('b' * 2) print('c' * 3) 
Output: a bb ccc