Open In App

Python – Random uppercase in Strings

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String, the task is to write a Python program to convert its characters to uppercase randomly.

Examples:

Input : test_str = 'geeksforgeeks'
Output : GeeksfORgeeks

Explanation : Random elements are converted to Upper case characters.

Input : test_str = 'gfg'
Output : GFg

Explanation : Random elements are converted to Upper case characters.

Method #1 : Using join() + choice() + upper() + lower()

In this, we perform the task of choosing random characters to upper case using choice() at each character. The upper() and lower() perform task of uppercasing and lowercasing characters respectively.

Python3




# Python3 code to demonstrate working of
# Random uppercase in Strings
# Using join() + choice() + upper() + lower()
from random import choice
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# choosing from upper or lower for each character
res = ''.join(choice((str.upper, str.lower))(char) for char in test_str)
 
# printing result
print("Random Uppercased Strings : " + str(res))


Output:

The original string is : geeksforgeeks
Random Uppercased Strings : gEEkSFoRgEeKS

Method #2 : Using map() + choice() + zip()

In this, we imply choice() over all the characters of lowercase and uppercase Strings joined (using zip()), using map().

Python3




# Python3 code to demonstrate working of
# Random uppercase in Strings
# Using map() + choice() + zip()
from random import choice
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# choosing from upper or lower for each character
# extending logic to each character using map()
res = ''.join(map(choice, zip(test_str.lower(), test_str.upper())))
 
# printing result
print("Random Uppercased Strings : " + str(res))


Output:

The original string is : geeksforgeeks
Random Uppercased Strings : geEkSFORgEEKS

Time Complexity: O(n) -> as join methods takes O(n)
Space Complexity: O(n)

Method #3 : Using random module and bytearray():

Approach:

This approach converts the string to a bytearray using the bytearray() function. Then, it selects the indices to convert to uppercase using random.sample(). Finally, it converts the bytes at those indices to uppercase by subtracting 32 from their ASCII values. The result is then decoded back to a string.

Python3




import random
 
def random_uppercase(string, num_uppercase):
    upper_indices = random.sample(range(len(string)), num_uppercase)
    string_bytes = bytearray(string, encoding='utf-8')
    for i in upper_indices:
        string_bytes[i] = string_bytes[i] - 32
    return string_bytes.decode('utf-8')
 
test_str = 'geeksforgeeks'
num_uppercase = 4
result = random_uppercase(test_str, num_uppercase)
print(result)


Output

geeKsfoRGeeKs

Time complexity: O(n + klogk), where n is the length of the string and k is the number of uppercase characters to generate.
Space complexity: O(n).

Method #4: Using reduce() method:

Algorithm :

  1. Initialize the input string test_str.
  2. Print the original string test_str.
  3. Generate a new string res using map() and choice(), where each character in the input string is randomly
  4. converted to uppercase or lowercase.
  5. Print the resulting random uppercase string res.

Python3




from random import choice
from functools import reduce
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# choosing from upper or lower for each character
# extending logic to each character using reduce()
res = reduce(lambda x, y: x + choice((y.lower(), y.upper())), test_str, '')
 
# printing result
print("Random Uppercased Strings : " + str(res))
#this code is contributed by Jyothi pinjala.


Output

The original string is : geeksforgeeks
Random Uppercased Strings : gEeKsfoRgeekS

Time Complexity:

The map() function iterates over each character in the input string test_str and applies the choice() function to each character, giving a time complexity of O(n), where n is the length of the input string.
The join() function has a time complexity of O(n), where n is the length of the resulting string.
Therefore, the overall time complexity of the code is O(n).

Space Complexity:

The space complexity of the code is O(n), where n is the length of the input string test_str. This is because we are creating a new string res of the same length as test_str.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads