0
0
Pythonprogramming~5 mins

String creation and representation in Python

Choose your learning style9 modes available
Introduction
Strings let us store and show words or sentences in programs. They help us work with text like names, messages, or any letters.
When you want to save a person's name in your program.
When you need to display a message to the user.
When you want to store a sentence or paragraph.
When you need to keep track of any text data like addresses or emails.
Syntax
Python
string_variable = 'Hello, world!'
string_variable = "Hello, world!"
string_variable = '''This is a
multi-line string.'''
string_variable = """This is also a
multi-line string."""
You can use single quotes (' ') or double quotes (" ") to create strings.
Triple quotes (''' ''' or """ """) let you write strings that span multiple lines.
Examples
A simple string using single quotes.
Python
greeting = 'Hi there!'
print(greeting)
Using double quotes and escaping inner quotes with backslash.
Python
quote = "She said, \"Hello!\""
print(quote)
A multi-line string using triple single quotes.
Python
multi_line = '''This is line one.
This is line two.'''
print(multi_line)
A multi-line string using triple double quotes.
Python
multi_line2 = """Line one.
Line two."""
print(multi_line2)
Sample Program
This program creates strings using single quotes and triple quotes. It also shows how to insert a variable inside a string using f-strings.
Python
name = 'Alice'
welcome_message = f"Hello, {name}! Welcome to the program."
print(welcome_message)

multi_line_text = '''This program shows how strings work.
You can write text in many ways.'''
print(multi_line_text)
OutputSuccess
Important Notes
Remember to use backslash (\) to include special characters like quotes inside strings.
Strings are always inside quotes, so forgetting them causes errors.
Triple quotes are great for writing long text or notes inside your code.
Summary
Strings store text inside quotes.
Use single, double, or triple quotes depending on your needs.
Triple quotes help write multi-line text easily.