0

I'm trying to create a program that involves assigning a string to the variable x depending on the length of the string assigned to variable y. The amount of commas in x should be the same as the length of y.

For example if e='h' , x=','. If e='hi' , x=',,'. If e='him' , x=',,,'

By the way, I'm new to programming so I don't know this stuff.

jpp
  • 159,742
  • 34
  • 281
  • 339
John
  • 11
  • 4
  • Possible duplicate of [Why is "if not someobj:" better than "if someobj == None:" in Python?](https://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python) – FisNaN Mar 23 '18 at 00:38

2 Answers2

2

Invoke len() on e to get the length of the string and multiply that length by the string ,, assigning it to x:

e = 'him'
e_length = len(e)
x = ',' * e_length
print(x)

This should yield ,,,.

Jason Ngo
  • 191
  • 1
  • 5
1

Python allows you to multiply a string by a number to repeat it, which means you can do:

e = 'hello there'
x = ',' * len(e)

Which will give you:

',,,,,,,,,,,'
Jeremy
  • 1,308
  • 10
  • 11