Python - Random uppercase in Strings
Last Updated :
18 Apr, 2023
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.
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
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)
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 :
- Initialize the input string test_str.
- Print the original string test_str.
- Generate a new string res using map() and choice(), where each character in the input string is randomly
- converted to uppercase or lowercase.
- 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.
OutputThe 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.
Similar Reads
Python - Uppercase Half String The problem is to convert half of a string to uppercase, either the first half or the second half, depending on the requirement. For example, given the string "python", the output could be "PYThon" (uppercase first half) or "pytHON" (uppercase second half).If the string length is odd, handle the mid
2 min read
Python - Every Kth Strings Uppercase Given a String list, change every Kth string to uppercase. Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 3 Output : ['GFG', 'is', 'best', 'FOR', 'geeks'] Explanation : All Kth strings are uppercased. Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 4 Output : ['GFG', '
4 min read
Python - Uppercase Selective Substrings in String Given a String, perform uppercase of particular Substrings from List. Input : test_str = 'geeksforgeeks is best for cs', sub_list = ["best", "geeksforgeeks"] Output : GEEKSFORGEEKS is BEST for cs Explanation : geeksforgeeks and best uppercased. Input : test_str = 'geeksforgeeks is best for best', su
7 min read
Python Program to Generate Random String With Uppercase And Digits Generating a series of random strings can help create security codes. Besides, there are many other applications for using a random string generator, for instance, obtaining a series of numbers for a lottery game or slot machines. A random string generator generates an alphanumeric string consisting
3 min read
Python - Random Replacement of Word in String Given a string and List, replace each occurrence of K word in string with random element from list. Input : test_str = "Gfg is x. Its also x for geeks", repl_list = ["Good", "Better", "Best"], repl_word = "x" Output : Gfg is Best. Its also Better for geeks Explanation : x is replaced by random repla
3 min read