What does

前端 未结 3 1633
忘掉有多难
忘掉有多难 2021-01-16 19:27

Going through string formatting operations but can\'t exactly wrap my head about %c operation and its use.

Learn Python the Hard Way, gave the following example but

3条回答
  •  [愿得一人]
    2021-01-16 20:27

    The %c is a format character gives the character representation. For example consider the following statements

    >>> print "%c" % 'a'
    a
    >>> print ("%c" % 97)
    a
    >>> print "%c" %'"'
    "
    >>> print "%c" %34
    "
    >>> print "%c" %'asdf'
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: %c requires int or char
    

    Breaking up

    "%c" % 34 == '"' 
    

    would be like

    >>> "%c" % 34
    "
    >> '"' == '"'
    True
    

提交回复
热议问题