Explorar o código

Add string replacement with regexp in python

Snow %!s(int64=8) %!d(string=hai) anos
pai
achega
764bd0f78e
Modificáronse 1 ficheiros con 14 adicións e 0 borrados
  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)