0
0
Pythonprogramming~5 mins

String values and text handling in Python

Choose your learning style9 modes available
Introduction

Strings let us work with words and sentences in programs. They help us store and change text like names, messages, or any written info.

When you want to store a person's name or address.
When you need to show messages or instructions on the screen.
When you want to change or check parts of a sentence.
When you read or write text files.
When you want to combine words or split sentences into smaller parts.
Syntax
Python
string_variable = "Hello, world!"

# You can use single or double quotes:
string1 = 'Hello'
string2 = "World"

# Multiline strings use triple quotes:
multiline = '''This is
multiple lines'''
Strings are put inside quotes: single (' '), double (" "), or triple (''' ''' or """ """).
Triple quotes let you write text on many lines without extra symbols.
Examples
This creates a string with single quotes and prints it.
Python
greeting = 'Hi there!'
print(greeting)
Double quotes let you include double quotes inside. Backslash \ escapes quotes inside strings.
Python
quote = "She said, \"Hello!\""
print(quote)
Triple quotes let you write text on multiple lines easily.
Python
poem = '''Roses are red,
Violets are blue.'''
print(poem)
Sample Program

This program shows how to create a string, add a name inside it, change it to uppercase, and split it into words.

Python
name = "Alice"
welcome_message = f"Hello, {name}! Welcome to the program."
print(welcome_message)

# Changing text
shout = welcome_message.upper()
print(shout)

# Splitting text
words = welcome_message.split()
print(words)
OutputSuccess
Important Notes

Use \n inside strings to start a new line.

Strings can be joined with + or repeated with *.

Use len(string) to find out how many characters a string has.

Summary

Strings hold text inside quotes.

You can change, join, split, and check strings easily.

Triple quotes help with writing text on many lines.