+ 10
22nd Mar 2018, 11:37 PM
Vučko/Вучко/Vuchko
Vučko/Вучко/Vuchko - avatar
4 Antworten
+ 7
Import the regular expression library 1: import re 2: Setup the string to process 3: str = "My name is David. Hi David." Create a pattern to toss 4: pattern = r"David" Overwrite the previous pattern with this one 5: pattern = r"." Create an updated string to toss 6: newstr = re.sub(pattern, "Amy", str) Overwrite the previous string with this one. Match every single character in the string we are processing and replace each one with 3 x's 7: newstr = re.sub(pattern, "xxx" , str) Print the resulting string of x's 8: print(newstr)
22nd Mar 2018, 11:56 PM
John Wells
John Wells - avatar
+ 3
😂 You really like to strange codes don’t you? I’m afraid I can’t help you, but by the looks of things, you’re a few steps ahead of me in python.
22nd Mar 2018, 11:54 PM
Jax
Jax - avatar
+ 3
Given the pattern '.' that matches any characters except newline, so each character in your 'str' variable is replaced by 'xxx'.
22nd Mar 2018, 11:55 PM
🌍 TATOU
+ 3
CODE WITH TRANSLATION: import re str = "My name is David. Hi David." # you created a string with 27 characters pattern = r"David" # you created a variable named 'pattern' with the raw string 'David' pattern = r"." # you changed the variable 'pattern' to '.' (this character matches any character except '\n') newstr = re.sub(pattern, "Amy", str) # you substituted every character in str to 'Amy' and stored the result in newstr # at this moment you have a string with 'Amy' repeating 27 times. newstr = re.sub(pattern, "xxx" , str) # you substitued every character in str to 'xxx' and stored the result in newstr again print(newstr) # you printed the value in newstr that is 'xxx' repeating 27 times Hope I my translation is easy to understand.
23rd Mar 2018, 12:06 AM
Ulisses Cruz
Ulisses Cruz - avatar