src/pylib/pystring/strbltins

Source   Edit  

Procs

func ascii(us: PyStr): PyStr {....raises: [], tags: [], forbids: [].}
Source   Edit  
func ascii(us: string): PyStr {....raises: [], tags: [], forbids: [].}

Example:

assert ascii("𐀀") == r"'\U00010000'"
assert ascii("đ") == r"'\u0111'"
assert ascii("和") == r"'\u548c'"
let s = ascii("v我\n\e")
when not defined(useNimCharEsc):
  let rs = r"'v\u6211\n\x1b'"
else:
  let rs = r"'v\u6211\n\e'"
assert s == rs
assert ascii("\"") == "'\"'"
assert ascii("\"'") == "'\"\\''"
let s2 = ascii("'")
when not defined(singQuotedStr):
  let rs2 = "\"'\""
else:
  let rs2 = r"'\''"
assert s2 == rs2
Source   Edit  
func bin(a`gensym13: SomeInteger): PyStr
Source   Edit  
func chr(a: SomeInteger): PyStr
Source   Edit  
func hex(a`gensym16: SomeInteger): PyStr
Source   Edit  
func oct(a`gensym10: SomeInteger): PyStr
Source   Edit  
proc ord(a: PyStr): int {....raises: [TypeError], tags: [], forbids: [].}
Raises TypeError if len(a) is not 1.

Example:

doAssert ord("ÎŽ") == 0x03b4
Source   Edit  
func ord1(a: PyStr): int {....raises: [], tags: [], forbids: [].}

Example:

assert ord1("123") == ord("1")
Source   Edit  
func pyrepr(s: StringLike): PyStr
Shortcut for str(pyreprImpl($s)))

Example:

# NOTE: string literal's `repr` is `system.repr`, as following. 
assert repr("\"") == "\"\\\"\""   # string literal of "\""
# use pyrepr for any StringLike and returns a PyStr
assert pyrepr("\"") == "'\"'"
Source   Edit  
func repr(x: PyStr): string {....raises: [], tags: [], forbids: [].}

Overwites system.repr for PyStr

The same as proc ascii except for unicode chars being remained AS-IS, and returns Nim's string.

Source   Edit  
func reversed(s: PyStr): PyStr {....raises: [], tags: [], forbids: [].}
Source   Edit  

Templates

template ascii(a: untyped): PyStr
As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using x, u, or U escapes

Example:

assert ascii(6) == "6"
Source   Edit  
template ascii(c: char): PyStr
we regard 'x' as a str (so as in python)

Example:

assert ascii('c') == "'c'"
Source   Edit