Browse Source

Add string replacement with regexp in python

Snow 8 years ago
parent
commit
764bd0f78e
1 changed files with 14 additions and 0 deletions
  1. 14 0
      arrays-and-strings/string-replacement.py

+ 14 - 0
arrays-and-strings/string-replacement.py

@@ -0,0 +1,14 @@
+import pytest
+import re
+
+def test_string_rep():
+    cases = ['', ' ', 'a', 'a s !']
+    expected = ['', '%20', 'a', 'a%20s%20!']
+    for c, e in zip(cases, expected):
+        assert string_rep(c) == e
+
+pytest.main()
+
+def string_rep(input):
+    p = re.compile(' ')
+    return p.sub('%20', input)