Python Strings

Strings are used to represent text data in Python. They are immutable, which means once a string is created, it cannot be changed.

Creating Strings

Strings can be created using single quotes ('), double quotes ("), or triple quotes (''' or """). For example:

single_quoted = 'Hello, Python!'
double_quoted = "Hello, Python!"
triple_quoted = '''Hello, Python!'''

String Concatenation

Strings can be concatenated using the + operator. For example:

first_name = 'John'
last_name = 'Doe'
full_name = first_name + ' ' + last_name

String Methods

Python provides many built-in methods for working with strings, such as upper(),lower(), strip(), replace(), and split(). For example:

message = '   Hello, Python!   '
print(message.strip())
print(message.upper())
print(message.replace('Hello', 'Hi'))
words = message.split(',')

String Formatting

String formatting allows you to create dynamic strings by inserting variables or expressions into a string. Python supports two main methods for string formatting: the format()method and f-strings. For example:

name = 'Alice'
age = 30
formatted_string = 'Name: {}, Age: {}'.format(name, age)
f_string = f'Name: {name}, Age: {age}'

Conclusion

Strings are a fundamental data type in Python, used for representing text data. By understanding how to create, manipulate, and format strings, you can perform a wide range of tasks in Python involving text processing.



[ Footer Placeholder]