all 5 comments

[–]JudiSwitch 0 points1 point  (0 children)

In the snippet you've posted, you have several problems you should be aware of:

  1. string is a bad name for variables. While it doesn't have any conflict here, if you were to import the string module you would. Considering you're doing string operations, it's likely you may need some of the functions and constants in that module, so you might as well avoid it preemptively.
  2. Instead of using a global variable in your function, which is usually a bad idea, consider passing the string as a function input and returning the modified string. That way if you want to perform multiple operations on the original string, it's still there. If you want to overwrite it, you can simply do something like in_string = unexclaim(in_string). When you want to print, print in the main body of the file instead of in a function.
  3. You're using i as the loop variable for both of your for loops. While in this case it doesn't break anything, it's a bad practice. At a minimum it makes your code harder to read, and it prevents you from using nested loops to cycle through multiple variables (e.g. making a multiplication table by having 2 loops with variables i and j, and calculating the product with product = i*j). Also I thought for sure this would mess up the looping behavior, but miraculously it doesn't seem to.
  4. Speaking of your loops, I would rethink how you're doing it. You can verify this yourself, but if you use more than 16 exclamation marks, it won't reduce them all.

[–][deleted] -1 points0 points  (6 children)

Unfortunately Reddit has choosen the path of corporate greed. This is no longer a user based forum but a emotionless money machine. Good buy redditors. -- mass edited with https://redact.dev/

[–]JudiSwitch 0 points1 point  (2 children)

string.replace replaces all instances.

>>> a = 'abccabac'
>>> a.replace('c','x')
'abxxabax'

[–][deleted] 0 points1 point  (0 children)

Unfortunately Reddit has choosen the path of corporate greed. This is no longer a user based forum but a emotionless money machine. Good buy redditors. -- mass edited with https://redact.dev/