Is there a more efficient / smarter way of randomizing the uppercasing of letters in a string ? Like this:
input_string = "this is my input string"
for i in range(10):
output_string = ""
for letter in input_string.lower():
if (random.randint(0,100))%2 == 0:
output_string += letter
else:
output_string += letter.upper()
print(output_string)
Output:
thiS iS MY iNPUt strInG
tHiS IS My iNPut STRInG
THiS IS mY Input sTRINg
This IS my INput STRING
ThIS is my INpUt strIng
tHIs is My INpuT STRInG
tHIs IS MY inPUt striNg
THis is my inPUT sTRiNg
thiS IS mY iNPUT strIng
THiS is MY inpUT sTRing
''.join(c.upper() if random() > 0.5 else c for c in input_string)timeiton the four ways (including mine) and your way seems to beat everyone by a large margin. (Couldn't get themapway to work though)''.join(c.upper() if random() > 0.5 else c for c in input_string.lower()), but checking the implementation actually I'm not surprisedrandom.choice()is slower (but more readable). You could also try''.join(lst[random.getrandbits(1)](c) for c in s). Also, should it be>=? @MarounMaroun relevant question: stackoverflow.com/questions/6824681/…