Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to create a String object
In Python, we can create a string object using Python's built-in function str() and also by assigning a sequence of characters to a variable. The sequence of characters is enclosed in single quotes, double quotes, or triple quotes for multiline strings. In this article, we look at the various ways to create string objects in Python.
Using Single Quotes
We can create a string object by simply assigning a sequence of characters enclosed in single quotes to a variable
my_string = 'Hello World!' print(my_string) print(type(my_string))
The output of the above code is
Hello World! <class 'str'>
Using Double Quotes
We can create a string object by enclosing a sequence of characters in double quotes and assigning it to a variable
my_string = "Hello World!" print(my_string) # Useful when string contains single quotes greeting = "It's a beautiful day!" print(greeting)
The output of the above code is
Hello World! It's a beautiful day!
Using Triple Quotes (Multiline Strings)
In Python we can create a multiline string object by enclosing the multiline string in triple quotes (single or double)
# Using triple single quotes
my_string = '''This is a multiline
String object
spanning multiple lines'''
print(my_string)
print("---")
# Using triple double quotes
poem = """Roses are red,
Violets are blue,
Python is awesome,
And so are you!"""
print(poem)
The output of the above code is
This is a multiline String object spanning multiple lines --- Roses are red, Violets are blue, Python is awesome, And so are you!
Using str() Function
The str() function can be used to convert any data type object to a string object. This is useful for type conversion
# Converting integer to string
my_number = 123
my_string = str(my_number)
print(f"String value: {my_string}")
print(f"Type: {type(my_string)}")
# Converting float to string
pi_value = 3.14159
pi_string = str(pi_value)
print(f"Pi as string: {pi_string}")
# Converting boolean to string
flag = True
flag_string = str(flag)
print(f"Boolean as string: {flag_string}")
The output of the above code is
String value: 123 Type: <class 'str'> Pi as string: 3.14159 Boolean as string: True
Comparison of String Creation Methods
| Method | Use Case | Example |
|---|---|---|
Single quotes '...'
|
Standard strings, contains double quotes | 'Hello "World"!' |
Double quotes "..."
|
Standard strings, contains single quotes | "It's working!" |
Triple quotes '''...'''
|
Multiline strings, docstrings | '''Line 1\nLine 2''' |
str() function |
Type conversion | str(123) |
Conclusion
Python offers multiple ways to create string objects: single quotes, double quotes for regular strings, triple quotes for multiline strings, and the str() function for type conversion. Choose the method based on your specific needs and content requirements.
