How to randomly select an item from a string in Python?


In this article, we will show you how to randomly select an item from a string using python. Below are the various methods in python to accomplish this task −

  • Using random.choice() method
  • Using random.randrange() method
  • Using random.randint() method
  • Using random.random()
  • Using random.sample() method
  • Using random.choices() method

Assume we have taken a string containing some elements. We will generate a random element from the given input string using different methods as specified above.

Method 1: Using random.choice() method

Algorithm (Steps)

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

  • Use the import keyword to import the random module(used to generate random integers. Because these are pseudo-random numbers, they are not actually random. This module can be used to generate random numbers, print a random value from a list or string, etc)

  • Create a string and add some dummy data to it.

  • Generate a random item from the string using random.choice() method(This function returns a random element from the specified sequence i.e string here) by passing the input string as an argument to the choice() function

  • Print the generated random string item.

Example

The following program returns a random element from the string using random.choice() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random item from the String using random.choice() method randomItem = random.choice(givenString) print("The generated random String item = ", randomItem)

Output

('The given input String: ', 'TutorialsPoint')
('The generated random String item = ', 't')

Method 2: Using random.randrange() method

Algorithm (Steps)

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

  • Generate a random index value from the string using random.randrange() method(Returns a random number within the specified range) by passing the length of the input string as an argument to it using the len() function(The number of items in an object is returned by the len() method)

  • Get the element present at the above index from the string and create a variable to store it.

Example

The following program returns a random element from the string using random.randrange() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random index value by passing the String length to the random.randrange() method randomIndex = random.randrange(len(givenString)) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)

Output

('The given input String: ', 'TutorialsPoint')
('The generated random String item = ', 'r')

Method 3: Using random.randint() method

Algorithm (Steps)

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

  • Generate a random index value from the string using random.randint() method(Returns a random number within the specified range) by passing the length of the input string as an argument to it using the len() function(The number of items in an object is returned by the len() method)

  • Get the element present at the above index from the string and create a variable to store it.

Example

The following program returns a random element from the string using random.randint() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random index value by passing String length as an argument # to the random.randint() function randomIndex = random.randint(0, len(givenString)-1) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)

Output

('The given input String: ', 'TutorialsPoint')
('The generated random String item = ', 'i')

Method 4: Using random.random()

Algorithm (Steps)

  • Generate a random float number using the random.random() function (Returns a float value at random between 0 and 1) and multiply it with the length of a string to get a random index and convert the result into an integer using the int() function(converts to an integer).

  • Get the element present at the above index from the string and create a variable to store it.

Example

The following program returns a random element from the string using random.random() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random float number and multiplying it with a length # of the String to get a random index and converting it into an integer randomIndex = int(random.random() * len(givenString)) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)

Output

('The given input String: ', 'TutorialsPoint')
('The generated random String item = ', 'n')

Method 5: Using random.sample() method

Algorithm (Steps)

  • Generate the required number of random items from the string using random.sample() method by passing the string, and the number of random items to be generated as arguments to it.

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

Syntax

random.sample(sequence, k)
  • Print the specified number of generated list of random string items.

Example

The following program returns n random elements from the string using random.sample() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating 3 random items from the String using random.sample() method randomItems = random.sample(givenString, 3) print("The generated 3 random String items = ", randomItems)

Output

('The given input String: ', 'TutorialsPoint')
('The generated 3 random String items = ', ['o', 'P', 'r'])

Method 6: Using random.choices() method

Algorithm (Steps)

  • Generate the required number of random items from the string using random.choices() method by passing the string, and number of random items to be generated (k) as arguments to it.

  • The random module contains the random.choices() method. It is useful to select multiple items from a string or a single item from a specific sequence.

Syntax

random.choices(sequence, k)
  • Print the specified number of generated random string items.

Example

The following program returns n random elements from the tuple using random.sample() method −

import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating 3 random items from String using random.choices() method randomItems = random.choices(givenString, k=3) print("The generated 3 random String items = ", randomItems)

Output

The given input String:  TutorialsPoint
The generated 3 random String items =  ['a', 'o', 'P']

Conclusion

We learned how to utilize the random module's various functions to select an item from a string.

Updated on: 27-Oct-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements