Python Program to Add K between case shifts

In Python, we can add a symbol between case shifts (transitions from uppercase to lowercase or vice versa) using built-in string methods like isupper(), islower(), and join(). A case shift occurs when consecutive characters change from uppercase to lowercase or lowercase to uppercase.

Understanding Case Shifts

Let's see what case shifts look like with an example ?

# Example of case shifts in a string
text = "tUtoRialsPoInt"
print("Original string:", text)
print("Case shifts occur between:")
print("t -> U (lowercase to uppercase)")
print("U -> t (uppercase to lowercase)")  
print("o -> R (lowercase to uppercase)")
Original string: tUtoRialsPoInt
Case shifts occur between:
t -> U (lowercase to uppercase)
U -> t (uppercase to lowercase)
o -> R (lowercase to uppercase)

Method 1: Using For Loop with isupper() and islower()

The isupper() method returns True if all characters are uppercase, while islower() returns True if all characters are lowercase ?

# Input string and symbol
input_string = 'tUtoRialsPoInt'
k = '#'

print("Input String:", input_string)

# Result string to store the output
result_str = ""

# Traverse through the string (excluding last character)
for i in range(len(input_string) - 1):
    current_char = input_string[i]
    next_char = input_string[i + 1]
    
    # Check for case shifts
    if (current_char.isupper() and next_char.islower()) or (current_char.islower() and next_char.isupper()):
        result_str += current_char + k
    else:
        result_str += current_char

# Add the last character
result_str += input_string[-1]

print("Resultant String after adding # between case shifts:", result_str)
Input String: tUtoRialsPoInt
Resultant String after adding # between case shifts: t#U#to#R#ials#P#o#I#nt

Method 2: Using List Comprehension

List comprehension provides a concise way to create lists and can be combined with join() to build strings ?

# Input string and symbol  
input_string = 'tUtoRialsPoInt'
k = '#'

print("Input String:", input_string)

# Use list comprehension to add k between case shifts
result_str = ''.join([
    input_string[i] + k 
    if (input_string[i].isupper() and input_string[i + 1].islower()) or 
       (input_string[i].islower() and input_string[i + 1].isupper())
    else input_string[i] 
    for i in range(len(input_string) - 1)
])

# Add the last character
result_str += input_string[-1]

print("Resultant String after adding # between case shifts:", result_str)
Input String: tUtoRialsPoInt
Resultant String after adding # between case shifts: t#U#to#R#ials#P#o#I#nt

Comparison

Method Readability Performance Best For
For Loop More readable Standard Beginners, complex logic
List Comprehension Concise Slightly faster Experienced developers, simple logic

Conclusion

Both methods effectively add symbols between case shifts using isupper() and islower() to detect transitions. The for loop approach is more readable for beginners, while list comprehension offers a more concise solution for experienced Python developers.

Updated on: 2026-03-27T12:58:36+05:30

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements