Home » Python programs

Python | Declare, assign and print the string (Different ways)



Here, we will learn how to declare, assign and print the string using different ways in Python?
Submitted by IncludeHelp, on August 01, 2018

Declare string object, assign string (using different ways), and print the string in Python.

These are some of the ways to assign strings to the string object.

Ways Syntax Description
Way1 Single quotes
'Message'
Assign a single line string.
Way2 Double quotes
"Message"
Assign a single line string.
Way3 Triple single quotes
'''Message'''
Assign single line as well as multi-line string.
Way4 Triple double quotes
"""Message"""
Assign single line as well as multi-line string.

Program:

# Declare, assign string (1)
# using single quotes 'string'
str1 = 'Hello world, How are you?'

# Declare, assign string (2)
# using double quotes "string"
str2 = "Hello world, How are you?"

# Declare assign string (3)
# using triple single quotes '''string'''
str3 = '''Hello world, How are you?'''

# Declare assign string (4)
# using triple double quotes """string"""
str4 = """Hello world, How are you?"""

# Declare, assign multi-line string (5)
# Triple double quotes allows to assign
# multi-line string
str5 = '''Hello world,
How are you?'''

# print the string
print "str1: ", str1
print "str2: ", str2
print "str3: ", str3 
print "str4: ", str4
print "str5: ", str5

Output

    str1:  Hello world, How are you?
    str2:  Hello world, How are you?
    str3:  Hello world, How are you?
    str4:  Hello world, How are you?
    str5:  Hello world,
    How are you?





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Quick links
Latest articles, Internship, Members
New...
Coding problems, Algorithms, Discrete Mathematics, Big data
Languages
C, C++, C++ STL, Java, Data Structure, C#.Net, Android, Kotlin, SQL
Web
PHP, Python, JavaScript, CSS, Ajax, Node.js, Web prog.
Programs
C, C++, DS, Java, C#, Python
Aptitude
C, C++, Java, DBMS
Interview
C, Embedded C, Java, SEO, HR
CS Subjects
CS Basics O.S. Networks DBMS Embedded Systems Cloud Computing Machine learning CS Organizations Linux DOS
More...
Articles, Puzzles, News/Updates


Recommended posts
C Tips & Tricks, C++ Tips & Tricks
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distros of 2018
C programming optimization techniques
Differences b/w C & Embedded C?
Embedded C Interview Q. & A.
C programming tips for Embedded Development
Basic rules of writing a C program
Important points (rules) to remember while writing C/C++ program
Top 5 Websites for solving programming challenges
Read more...


Others...
Computer G.K. (MCQ)
Most viewed pages...
Categories...



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.