InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    python print examples

    By admin on Jan 2, 2016

    Here are some examples you can print strings or variables in python on stdout.

    Print a string with newline in the end

    By default python will append a newline in the end.

    print "Hello\n"
    print "Hello2"
    s = "Hello3"
    print s
    Hello
    
    Hello2
    Hello3
    
    Env: Python 2.7.18

    Print a string without newline

    One can use sys.stdout.write to print without newline

    import sys
    sys.stdout.write("Hello-")
    sys.stdout.write("World\n")
    Hello-World
    
    Env: Python 2.7.18

    Print multiple strings with comma

    Multiple strings or variables can be printed using comma and it will add space between the strings and newline after last string.
    We can use sys.stdout.write to print without newline

    v1 = "Hello"
    print v1, "Hello2"
    Hello Hello2
    
    Env: Python 2.7.18

    Concatenate and print multiple strings with plus

    In case a variable is not of string type, it needs to be converted to string using str()

    print "Hello" + " World"
    i = 2
    print "Hello" + str(i)
    Hello World
    Hello2
    
    Env: Python 2.7.18

    Print formatted string

    Using %s will print arguments to string if needed using str(). You can also use %d to print integers. A detailed list can be seen at python site – string formatting operations.

    s = "Hello"
    i = 2
    print "%s %s %d" % (s, i, i)
    Hello 2 2
    
    Env: Python 2.7.18

    Suggested posts:

    1. Python string split examples
    2. Python find length of string or list using len()
    3. HTML5 drag and drop – move an element to dropzone
    4. HTML5 template tag hello world tutorial
    5. Python/Perl/Unix one liners
    6. AngularJS format date using date filter
    7. Bash – set default value if a variable is empty
    8. CSS max-height – limit maximum height of an element
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python built-in functions, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress