Advanced

Strings

Explore how to create, manipulate, and format strings using Python’s built-in syntax and string methods.

Strings

Strings are one of the most commonly used data types in Python.

Creating Strings

pythonCopyEditname = "Alice"
greeting = 'Hello'

Python supports both single (') and double (") quotes.

Multiline Strings

Use triple quotes for multiline strings:

pythonCopyEditmessage = """This is
a multi-line
string."""

String Concatenation

pythonCopyEditfirst = "Hello"
second = "World"
combined = first + " " + second
print(combined)  # Output: Hello World

String Interpolation

Use f-strings (Python 3.6+):

pythonCopyEditname = "Alice"
print(f"Hello, {name}!")

Common String Methods

pythonCopyEdittext = "Python is fun"

text.upper()       # 'PYTHON IS FUN'
text.lower()       # 'python is fun'
text.startswith("Py")  # True
text.replace("fun", "awesome")  # 'Python is awesome'

String Slicing

pythonCopyEditword = "Python"
print(word[0:3])   # 'Pyt'
print(word[-1])    # 'n'

Strings are immutable, meaning their content cannot be changed in place.

Strings

Strings are one of the most commonly used data types in Python.

Creating Strings

pythonCopyEditname = "Alice"
greeting = 'Hello'

Python supports both single (') and double (") quotes.

Multiline Strings

Use triple quotes for multiline strings:

pythonCopyEditmessage = """This is
a multi-line
string."""

String Concatenation

pythonCopyEditfirst = "Hello"
second = "World"
combined = first + " " + second
print(combined)  # Output: Hello World

String Interpolation

Use f-strings (Python 3.6+):

pythonCopyEditname = "Alice"
print(f"Hello, {name}!")

Common String Methods

pythonCopyEdittext = "Python is fun"

text.upper()       # 'PYTHON IS FUN'
text.lower()       # 'python is fun'
text.startswith("Py")  # True
text.replace("fun", "awesome")  # 'Python is awesome'

String Slicing

pythonCopyEditword = "Python"
print(word[0:3])   # 'Pyt'
print(word[-1])    # 'n'

Strings are immutable, meaning their content cannot be changed in place.

Strings

Strings are one of the most commonly used data types in Python.

Creating Strings

pythonCopyEditname = "Alice"
greeting = 'Hello'

Python supports both single (') and double (") quotes.

Multiline Strings

Use triple quotes for multiline strings:

pythonCopyEditmessage = """This is
a multi-line
string."""

String Concatenation

pythonCopyEditfirst = "Hello"
second = "World"
combined = first + " " + second
print(combined)  # Output: Hello World

String Interpolation

Use f-strings (Python 3.6+):

pythonCopyEditname = "Alice"
print(f"Hello, {name}!")

Common String Methods

pythonCopyEdittext = "Python is fun"

text.upper()       # 'PYTHON IS FUN'
text.lower()       # 'python is fun'
text.startswith("Py")  # True
text.replace("fun", "awesome")  # 'Python is awesome'

String Slicing

pythonCopyEditword = "Python"
print(word[0:3])   # 'Pyt'
print(word[-1])    # 'n'

Strings are immutable, meaning their content cannot be changed in place.

© Layer Docs

Made by Alejandro · Powered by Framer

Create a free website with Framer, the website builder loved by startups, designers and agencies.