Python Program to Random uppercase in Strings


In this article, we will learn how to convert characters to uppercase in Strings Randomly using python.

Methods Used

The following are the various methods to accomplish this task −

  • Using join(), choice(), lower(), upper() functions.

  • Using random.randInt() and random.sample() functions.

  • Using map(), choice(), zip() functions

Example

Assume we have taken an input string. We will now randomly convert the characters of an input string to uppercase using the above methods.

Input

inputString = 'tutorialspoint'

Output

String after converting chars lower or uppercase randomly:
tUToriaLsPOINT

In this example, We are converting characters of an input string 'tutorialspoint' to uppercase randomly.

Method 1: Using join(), choice(), lower, upper functions

lower − converts all uppercase characters in a string to lowercase characters.

random.choice() function(randomly selects an item from a sequence like a list, tuple, range of numbers).

upper − converts all lowercase characters in a string to uppercase characters.

join() − join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task –.

  • Use the import keyword to import the choice function from the random module.

  • Create a variable to store the input string.

  • Print the input string.

  • Traverse through each character of an input string and select either upper or lowercase characters randomly using random.choice(). Here join is used to convert to a string

  • Print the resultant string after converting characters of an input string to uppercase randomly

Example

The following program returns a string after converting characters of an input string to uppercase randomly using join(), random.choice(), lower, and upper functions –

# importing choice from the random module
from random import choice

# input string
inputString = 'tutorialspoint'

# printing input string
print("Input String:", inputString)

# traversing through each character of a string and selecting

# either upper or lowercase char randomly using choice()

# here join is used to convert to a string
randchars_str = ''.join(choice((str.upper, str.lower))(char) for char in inputString)
   
# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", randchars_str)

Output

On executing, the above program will generate the following output –

Input String: tutorialspoint
String after converting chars lower or uppercase randomly:
tUTORIAlSPOInT

Method 2: Using random.randInt() and random.sample() functions

random.randint() method: Returns a random number within the specified range.

random.sample() method : It returns a list containing a randomly selected number of elements from a sequence.

Syntax

random.sample(sequence, k)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Convert the given input string to list using the list() function.

  • Calculate the number of random indices that need to be converted to uppercase using random.randInt() function.

  • Get above n number of random elements from 0 to the length of the list by passing the length of list and number of elements needed as arguments to random.sample() function.

  • Save the above indices to a list say randomIndexList.

  • Traverse through the above randomIndexList using for loop.

  • Convert the string element at that index to upper case using the upper() function.

  • Join all the list elements and Convert to string using the join() function.

  • Print the result.

Example

The following program returns a string after converting characters of an input string to uppercase randomly using –

import random
# input string
inputString = 'tutorialspoint'

# printing input string
print("Input String:", inputString)

# Converting string to list
stringList = list(inputString)

# Getting the number of random indexes to be modified to upper case
n = random.randint(0, len(stringList)-1)

# Getting above n number of random elements from 0 to the length of the list
randomIndexList = random.sample(range(0, len(stringList)-1), n)

# Traversing the above index list
for index in randomIndexList:
   
   # Converting the corresponding index string element to the upper case
   stringList[index] = stringList[index].upper()

# Joining all the list elements and Converting to string
result = ''.join(stringList)

# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", result)

Output

On executing, the above program will generate the following output –

Input String: tutorialspoint
String after converting chars lower or uppercase randomly:
tUToriaLsPOINT

Method 3: Using map(), choice(), zip() functions

random.choice() function(randomly selects an item from a sequence like a list, tuple, range of numbers).

random.choice(sequence)

zip() function is used to combine two lists/iterators.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword to import the choice function from the random module.

  • Zip the lower and upper characters of the string using the zip function and select any one from this using the random.choice() method.

  • Apply this to all the elements of the string using the map() function.

  • Convert this map object to string using the join() function.

  • Print the resultant string after converting characters of an input string to uppercase randomly.

Example

The following program returns a string after converting characters of an input string to uppercase randomly using map(), random.choice(), and zip() functions –

# importing choice from the random module
from random import choice

# input string
inputString = 'tutorialspoint'
print("Input String:", inputString)

# extending logic to each character using a map,

# and selecting upper or lower for each character
randchars_str = ''.join(map(choice, zip(inputString.lower(), inputString.upper())))

# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", randchars_str)

Output

On executing, the above program will generate the following output –

Input String: tutorialspoint
String after converting chars lower or uppercase randomly:
TutoRIaLsPoInt

Conclusion

We learned how to convert Random uppercase in Strings using three different approaches in this article. We also learned how to generate a random integer with randInt(), generate n random numbers as a list with sample(), and choose one item at random with choice() .

Updated on: 27-Jan-2023

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements