Python – Uppercase Half String
Last Updated :
08 May, 2023
Given a String, perform uppercase of the later part of the string.
Input : test_str = 'geeksforgeek'
Output : geeksfORGEEK
Explanation : Latter half of string is uppercased.
Input : test_str = 'apples'
Output : appLES
Explanation : Latter half of string is uppercased.
Method #1 : Using upper() + loop + len()
In this, we compute the half index and then perform upper() to only those characters which lie in the other half of the string.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = ''
for idx in range ( len (test_str)):
if idx > = hlf_idx:
res + = test_str[idx].upper()
else :
res + = test_str[idx]
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #2 : Using list comprehension + join() + upper()
This is similar to the above method, just the task is performed in a shorthand manner using list comprehension.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = ''.join([test_str[idx].upper() if idx > = hlf_idx else test_str[idx]
for idx in range ( len (test_str))])
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using String slicing and upper()
Use string slicing to divide the string into two separate sub_string and convert the right sub_string to the upper case with upper() method.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = test_str[:hlf_idx] + test_str[hlf_idx:].upper()
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time Complexity: O(n) -> (slicing)
Auxiliary Space: O(n)
Method #4 : Using slicing +index()+for loop
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
x = len (test_str) / / 2
a = test_str[x:]
b = test_str[:x]
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
sma = "abcdefghijklmnopqrstuvwxyz"
res = ""
for i in a:
res + = cap[sma.index(i)]
res = b + res
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n) as we are creating a new string ‘res’ with a length equal to the length of the input string.
Method#5: using re module
Steps:
- Import the re module.
- Define a string to be processed, test_str.
- Print the original string.
- Compute the half index of the string by dividing its length by 2.
- Extract the second half of the string using the half index.
- Use regular expressions to convert all word characters in the second half of the string to uppercase.
- a. Use (?i) to set the regular expression to case-insensitive mode.
- b. Use \w+ to match all word characters in the string.
- c. Use a lambda function to convert each match to uppercase using the group() method of the match object.
- d. Replace all matches in the second half of the string with their uppercase equivalents using re.sub().
- Concatenate the first half of the string with the modified second half to create the result string.
- Print the result string.
Python3
import re
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = re.sub(r "(?i)(\w+)" , lambda m: m.group().upper(), test_str[hlf_idx:])
result = test_str[:hlf_idx] + res
print ( "The resultant string : " + str (result))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time complexity: O(n), where n is the length of the input string. This is because the regular expression pattern is applied to the second half of the string, which has length n/2. The time complexity of re.sub() is proportional to the length of the input string, so the overall time complexity is O(n/2 + n/2) = O(n).
Auxiliary space: O(n), where n is the length of the input string. This is because the re.sub() function creates a new string containing the modified second half of the input string, and the result string also has length n. The space used by the other variables (e.g., test_str, hlf_idx, res) is constant, regardless of the length of the input string.
Method #6: Using string concatenation and conditional operator
Steps:
- Initialize the string variable test_str.
- Calculate the length of the string and store it in the length variable.
- Initialize an empty string variable result.
- Use a for loop to iterate through the string characters.
- Use a conditional operator to check if the index of the character is less than half of the string length.
- If the above condition is true, concatenate the character with the result variable.
- If the above condition is false, concatenate the uppercase of the character with the result variable.
- Print the result variable.
Below is the implementation of the above approach:
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
length = len (test_str)
hlf_idx = length / / 2
result = ''
for i in range (length):
result + = test_str[i] if i < hlf_idx else test_str[i].upper()
print ( "The resultant string : " + str (result))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as we are using an extra variable result to store the output string.
Method#7:Using itertools, islice(), and string concatenation:
Algorithm :
- Initialize the input string test_str and compute its half index hlf_idx.
- Use the islice function from the itertools module to iterate over the characters of test_str from the beginning until the end, and assign each character to the variable c.
- For each character c, use a list comprehension to:
- Check if its index i is greater than or equal to hlf_idx.
- If so, convert the character to uppercase using the upper() method and append it to a list.
- If not, append the character as is to the list.
- Use the join() method to concatenate the list of characters into a new string res.
- Print the new string res.
Python3
from itertools import islice
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = "".join([c.upper() if i > = hlf_idx else c for i, c in enumerate (islice(test_str, None ))])
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORGEEKS
Time complexity: O(n), where n is the length of the input string test_str. This is because the code iterates over the characters of the string exactly once, and each iteration takes constant time.
Auxiliary space: O(N), where n is the length of the input string test_str. This is because the code creates a list of the characters of the string and then joins them back into a string, using an additional list to hold the characters of the new string. Both the original and the new string require O(n) space. The islice object created by the islice function also requires O(1) space.
Method 8: Using built-in function map() + the ternary operator.
Steps:
- Get the length of the string and find the half index by dividing it by 2 and using the floor division operator to get an integer value.
- Use map() to apply the ternary operator on each character of the string. If the index of the character is greater than or equal to the half index, return the character in uppercase, otherwise return the original character.
- Convert the map object to a string using the join() method.
- Assign the resultant string to the variable res.
- Print the original string and the resultant string.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
hlf_idx = len (test_str) / / 2
res = ''.join( map ( lambda c: c.upper() if test_str.index(c) > = hlf_idx else c, test_str))
print ( "The resultant string : " + str (res))
|
Output
The original string is : geeksforgeeks
The resultant string : geeksfORgeeks
Time complexity: O(N), where n is the length of the string.
Auxiliary space: O(N) because the new string is created in memory.
Please Login to comment...