193k views
4 votes
Create a function generateString(char, val) that returns a string with val number of char characters concatenated together.

For example generateString('a', 7) will return aaaaaaa.

# Get our arguments from the command line
import sys
character= sys.argv[1]
count= int(sys.argv[2])

# Your code goes here

User AsgarAli
by
4.8k points

1 Answer

7 votes

Answer:

Here is the function generateString() which has two parameters char for storing characters of a string and val is a number in order to return string with val number of char characters.

def generateString(char, val):

result = char * val

return result

If you want to see if the function works correct and generates right output you can check it by calling the generateString() function and passing values to it and printing the result using print() function as following:

print(generateString('a',7))

This will produce the following output:

aaaaaaa

Step-by-step explanation:

The complete program is:

import sys

character= sys.argv[1]

count= int(sys.argv[2])

# Your code goes here

def generateString(char, val):

result = char * val

return result

print(character*count)

The logic of this function generateString(char, val) is explained below.

Lets say the value of char = a and val = 3 This means that generateString() should return 3 a's concatenated together. This concatenation is done by multiplication of the value of char i.e. 3 with the value of val i.e. 3.

So printing a 3 times displays aaa

User Mthakuri
by
5.3k points