DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (23)

Buy Me a Coffee

*Memos:

f-strings can format a string as shown below. *Format Specification can be used with f-strings:

*Memos:

  • f or F with '' or "" must be used.

<{variable}>:

*Memos:

  • A {} can output a value.
  • A {} must have an expression with zero or more spaces.
  • A {} can have = with zero or more spaces after an expression to output the value including its expression.
  • If using =, the spaces in the expression and around = in {} are included in the value.
  • If not using =, the spaces in the expression in {} aren't included in the value.
v1 = 'A'
v2 = 'B'
v3 = 'C'

print(f'-{v1}-{v2}-{v3}-')
print(F"-{v1}-{v2}-{v3}-")
print(f'-{ v1 }-{  v2  }-{    v3    }-')
print(F"-{ v1 }-{  v2  }-{    v3    }-")
# -A-B-C-

print(f'-{v1=}-{v2=}-{v3=}-')
print(F"-{v1=}-{v2=}-{v3=}-")
# -v1='A'-v2='B'-v3='C'-

        # ↓  ↓ ↓   ↓↓  ↓↓ ↓↓   ↓↓↓↓  ↓↓↓↓ ↓↓↓↓
print(f'-{ v1 = }-{  v2  =  }-{    v3    =    }-')
print(F"-{ v1 = }-{  v2  =  }-{    v3    =    }-")
# - v1 = 'A'-  v2  =  'B'-    v3    =    'C'-
#  ↑  ↑ ↑    ↑↑  ↑↑ ↑↑    ↑↑↑↑  ↑↑↑↑ ↑↑↑↑

print(f'{'ABC'} {100} {True}')
print(F"{'ABC'} {100} {True}")
# ABC 100 True

print(f'')
print(F"")
# Nothing

print(f'{}')
print(F"{}")
print(f'{ }')
print(F"{ }")
# SyntaxError: f-string: valid expression required before '}'
Enter fullscreen mode Exit fullscreen mode
v1 = 'AB'
v2 = 4

print(f'-{v1+'CDE'}-{3*v2}-')
print(f'-{ v1 + 'CDE' }-{ 3 * v2 }-')
print(f'-{  v1  +  'CDE'  }-{  3  *  v2  }-')
print(f'-{    v1    +    'CDE'    }-{    3    *    v2    }-')
# -ABCDE-12-

print(f'-{v1+'CDE'=}-{3*v2=}-')
# -v1+'CDE'='ABCDE'-3*v2=12-

#         ↓  ↓ ↓     ↓ ↓   ↓ ↓ ↓  ↓ ↓
print(f'-{ v1 + 'CDE' = }-{ 3 * v2 = }-')
# - v1 + 'CDE' = 'ABCDE'- 3 * v2 = 12-
#  ↑  ↑ ↑     ↑ ↑        ↑ ↑ ↑  ↑ ↑ 

#         ↓↓  ↓↓ ↓↓     ↓↓ ↓↓   ↓↓ ↓↓ ↓↓  ↓↓ ↓↓
print(f'-{  v1  +  'CDE'  =  }-{  3  *  v2  =  }-')
# -  v1  +  'CDE'  =  'ABCDE'-  3  *  v2  =  12-
#  ↑↑  ↑↑ ↑↑     ↑↑ ↑↑        ↑↑ ↑↑ ↑↑  ↑↑ ↑↑

#         ↓↓↓↓  ↓↓↓↓ ↓↓↓↓     ↓↓↓↓ ↓↓↓↓   ↓↓↓↓ ↓↓↓↓ ↓↓↓↓  ↓↓↓↓ ↓↓↓↓
print(f'-{    v1    +    'CDE'    =    }-{    3    *    v2    =    }-')
# -    v1    +    'CDE'    =    'ABCDE'-    3    *    v2    =    12-
#  ↑↑↑↑  ↑↑↑↑ ↑↑↑↑     ↑↑↑↑ ↑↑↑↑        ↑↑↑↑ ↑↑↑↑ ↑↑↑↑  ↑↑↑↑ ↑↑↑↑
Enter fullscreen mode Exit fullscreen mode
print(f'-{'AB'+'CDE'}-{3*4}-')
print(f'-{ 'AB' + 'CDE' }-{ 3 * 4 }-')
print(f'-{  'AB'  +  'CDE'  }-{  3  *  4  }-')
print(f'-{    'AB'    +    'CDE'    }-{    3    *    4    }-')
# -ABCDE-12-

print(f'-{'AB'+'CDE'=}-{3*4=}-')
# -'AB'+'CDE'='ABCDE'-3*4=12-

#         ↓    ↓ ↓     ↓ ↓   ↓ ↓ ↓ ↓ ↓
print(f'-{ 'AB' + 'CDE' = }-{ 3 * 4 = }-')
# - 'AB' + 'CDE' = 'ABCDE'- 3 * 4 = 12-
#  ↑  ↑ ↑ ↑     ↑ ↑        ↑ ↑ ↑ ↑ ↑ 

#         ↓↓    ↓↓ ↓↓     ↓↓ ↓↓   ↓↓ ↓↓ ↓↓ ↓↓ ↓↓
print(f'-{  'AB'  +  'CDE'  =  }-{  3  *  4  =  }-')
# -  'AB'  +  'CDE'  =  'ABCDE'-  3  *  4  =  12-
#  ↑↑    ↑↑ ↑↑     ↑↑ ↑↑        ↑↑ ↑↑ ↑↑ ↑↑ ↑↑

#         ↓↓↓↓    ↓↓↓↓ ↓↓↓↓     ↓↓↓↓ ↓↓↓↓   ↓↓↓↓ ↓↓↓↓ ↓↓↓↓ ↓↓↓↓ ↓↓↓↓
print(f'-{    'AB'    +    'CDE'    =    }-{    3    *    4    =    }-')
# -    'AB'    +    'CDE'    =    'ABCDE'-    3    *    4    =    12-
#  ↑↑↑↑    ↑↑↑↑ ↑↑↑↑     ↑↑↑↑ ↑↑↑↑        ↑↑↑↑ ↑↑↑↑ ↑↑↑↑ ↑↑↑↑ ↑↑↑↑
Enter fullscreen mode Exit fullscreen mode

*Indentation is possible between { and }.

v = 'CD'

print(f'AB{
      v + 'EF'}')
print(f'AB{v
      + 'EF'}')
print(f'AB{v +
      'EF'}')
print(f'AB{v + 'EF'
      }')
# ABCDEF

print(f'
      AB{v + 'EF'}')
print(f'AB
      {v + 'EF'}')
print(f'AB{v + 'EF'}
      ')
# SyntaxError: unterminated f-string literal
Enter fullscreen mode Exit fullscreen mode

*Format Specification can be used with f-strings as I explain it with f-strings in my post:

v = -0.0

print(f'"{v:0>-z#010_.0f}"')
print(f'"{v:0>-z#10_.0f}"')
print(f'"{v:>-z#010_.0f}"')
# "000000000."

print(f'"{v:0>-z#010_.0f}" "{v:0>-z#10_.0f}" "{v:>-z#010_.0f}"')
print(f'"{v:0>-z#010_.0f}" "{v:0>-z#10_.0f}" "{v:>-z#010_.0f}"')
# "000000000." "000000000." "000000000."
Enter fullscreen mode Exit fullscreen mode

<{variable[index]}>:

v = ['A', 'B', 'C']

print(f'-{v}-{v[0]}-{v[1]}-{v[2]}-')
# -['A', 'B', 'C']-A-B-C-
Enter fullscreen mode Exit fullscreen mode
v = [['A', 'B'], ['C', 'D']]

print(f'-{v}\n-{v[0]}-{v[1]}\n-{v[0][0]}-{v[0][1]}-{v[1][0]}-{v[1][1]}-')
# -[['A', 'B'], ['C', 'D']]
# -['A', 'B']-['C', 'D']
# -A-B-C-D-
Enter fullscreen mode Exit fullscreen mode

<{variable[key]}>:

*key cannot omit "" or ''.

v = {'name':'John', 'age':36}

print(f'-{v}\n-{v['name']}-{v['age']}-')
# -{'name': 'John', 'age': 36}
# -John-36-

print(f'{v[name]}')
# NameError: name 'name' is not defined

print(f'{v[age]}')
# NameError: name 'age' is not defined
Enter fullscreen mode Exit fullscreen mode
v = {'person':{'name':'John', 'age':36}}

print(f'-{v}\n-{v['person']}\n-{v['person']['name']}-{v['person']['age']}-')
# -{'person': {'name': 'John', 'age': 36}}
# -{'name': 'John', 'age': 36}
# -John-36-
Enter fullscreen mode Exit fullscreen mode

<variable.key & variable.key()>:

import math

v = math

print(f'{v.pi}')
# 3.141592653589793

print(f'{v.pow}')
# <built-in function pow>

print(f'{v.pow(3, 2)}')
# 9.0
Enter fullscreen mode Exit fullscreen mode

<!s & !r & !a>:

*Memos:

  • !s is str() which can create a string.
  • !r is repr() which can create a printable string.
  • !a is ascii() which can create the printable string of ASCII characters. *Non-ASCII characters are represented with \x, \u, or \U escape.
v = "Jφhи\tSмiтh"

print(f"{v!s}")
# Jφhи    Sмiтh

print(f"{v!r}")
# 'Jφhи\tSмiтh'

print(f"{v!a}")
# 'J\u03c6h\u0438\tS\u043ci\u0442h'
Enter fullscreen mode Exit fullscreen mode
v = 'I\'m Jφhи\tSмiтh.'

print(f'{v!s}')
# I'm Jφhи    Sмiтh.

print(f'{v!r}')
# "I'm Jφhи\tSмiтh."

print(f'{v!a}')
# "I'm J\u03c6h\u0438\tS\u043ci\u0442h."
Enter fullscreen mode Exit fullscreen mode

<{{ & }}>

*Memos:

  • { can escape {.
  • } can escape }.
print(f'The bracket {{ is escaped.')
# The bracket { is escaped.

print(f'The bracket }} is escaped.')
# The bracket } is escaped.

print(f'The {{brackets}} are escaped.')
# The {brackets} are escaped.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)