当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

Python 字符串填充

  1. lines_of_text = [
  2. (123, 5487, 'Testing', 'Billy', 'Jones'),
  3. (12345, 100, 'Test', 'John M', 'Smith')
  4. ]
  5. for mytuple in lines_of_text:
  6. name = '{}, {}'.format(mytuple[4], mytuple[3])
  7. value = '$' + str(mytuple[1])
  8. print('{name:<20} {id:>8} {test:<12} {value:>8}'.format(
  9. name=name, id=mytuple[0], test=mytuple[2], value=value)
  10. )

Output:

  1. Jones, Billy 123 Testing $5487
  2. Smith, John M 12345 Test $100