Skip to content

Commit 0f73c74

Browse files
Merge pull request RustPython#1032 from michelhe/printf-like-string-formatting
Printf-style formatting
2 parents 73edde6 + 64af5c4 commit 0f73c74

6 files changed

Lines changed: 1053 additions & 1 deletion

File tree

tests/snippets/strings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ def __repr__(self):
204204
assert "{foo} {foo!s} {foo!r} {foo!a}".format(foo=f) == 'str(Foo) str(Foo) repr(Foo) repr(Foo)'
205205
# assert '{} {!r} {:10} {!r:10} {foo!r:10} {foo!r} {foo}'.format('txt1', 'txt2', 'txt3', 'txt4', 'txt5', foo='bar')
206206

207+
208+
# Printf-style String formatting
209+
assert "%d %d" % (1, 2) == "1 2"
210+
assert "%*c " % (3, '❤') == " ❤ "
211+
assert "%(first)s %(second)s" % {'second': 'World!', 'first': "Hello,"} == "Hello, World!"
212+
assert "%(key())s" % {'key()': 'aaa'}
213+
assert "%s %a %r" % (f, f, f) == "str(Foo) repr(Foo) repr(Foo)"
214+
assert "repr() shows quotes: %r; str() doesn't: %s" % ("test1", "test2") == "repr() shows quotes: 'test1'; str() doesn't: test2"
215+
216+
assert_raises(TypeError, lambda: "My name is %s and I'm %(age)d years old" % ("Foo", 25), msg="format requires a mapping")
217+
assert_raises(TypeError, lambda: "My name is %(name)s" % "Foo", msg="format requires a mapping")
218+
assert_raises(ValueError, lambda: "This %(food}s is great!" % {"food": "cookie"}, msg="incomplete format key")
219+
assert_raises(ValueError, lambda: "My name is %" % "Foo", msg="incomplete format")
220+
207221
assert 'a' < 'b'
208222
assert 'a' <= 'b'
209223
assert 'a' <= 'a'

0 commit comments

Comments
 (0)