Search this site
Embedded Files
Python by Examples
  • Tutorial
    • Download & Install
      • Install Python on Ubuntu Linux
    • Help
  • Strings
    • comparison
    • Get safe character string
    • str() and repr()
  • argparse
    • ValueError
  • Biopython
    • Examples
      • GC content
      • Inverse seq
      • Read/write fasta
    • Install Biopython
  • Data structures
    • Dictionary
      • get keys
      • Multiple keys
    • Lists
      • 0-based indices
      • List element frequencies
      • Median
  • Debugging
  • Error
    • TabError
  • Error handling
    • Check Python version
    • Type checking (raise)
  • File operations
    • File commands
      • get module path
      • os.rename vs shutil.move
      • tar
    • Files: read & write
      • gzip compression
      • Read csv files
      • strip
  • Functions
  • Loops
    • For loops
    • if else ...
    • List Comprehension
  • Math
    • DataFrame
    • Machine Learning
      • Kernel PCA
    • Matrix Calculations
      • Plot
    • Statistics
    • types
  • Packages
    • Create own modules
    • Import Modules
      • Check modul version
    • Install packages
    • python script
  • Parallel processing
    • pool.map - multiple arguments
  • Plot
    • barplot
    • colors
      • rgb2hex
    • matplotlib
      • colormaps
      • Error
      • Transparent colors
  • Print
    • Error
  • System
    • Environment
    • External Commands
  • Time
  • WWW
    • CGI script
    • Download webpage
    • Graphics
Python by Examples

Print

Python by Examples

print two strings with space between

>>> s1 = 'Hello'; s2 = 'world'

>>> print(s1 + ' ' + s2)

Hello world

better with python3 syntax

>>> from __future__ import print_function

>>> print(s1, s2, sep=' ')

Hello world

print centered '^' within line length of 40 letters (left: '<'  right: '>')

>>> print(format(s1,'^40'))

                      Hello  

>>> print(format(s1,'>40'))

                                   Hello

Print floating-point numbers

# left aligned of 10 digit line length with two digits after point (rounded)

x=65.246

print(format(x,'10.2f'))

65.25

# rounded 2 digits after comma, but without leading spaces

format(x,'.2f') 

65.25

'{:.2f}'.format(x)

'65.25'

'{:06.2f}'.format(x)

'065.25'

Print list elements

strings and numbers

>>> mylist = ['samples', 165]

>>> print(mylist)

['samples', 165]

a) apply the list as separate arguments, print converts each element to a string

>>> print(*mylist)

samples 165

>>> print(*mylist, sep=': ') # specify own separator

samples: 165

b) convert all numbers to strings before joining

>>> print(': '.join(str(l) for l in mylist))

samples: 165

https://pyformat.info/

Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse