- Author

- Name
- Nelson Silva
- Social
Introduction
Strings are one of the most widely used data structures in programming, serving to store and manipulate text. In Python, strings offer a variety of methods that allow us to perform common operations efficiently.
Escape Characters
Escape characters allow you to represent characters that cannot be written directly. The most common ones are:
\n: Represents a new line.\t: Represents a tab.
text = "This is a string\nwith two lines."
print(text)
'''
This is a string
with two lines.
'''
Common String Methods
Python offers a variety of methods that can be used to manipulate and query strings:
1. len()
The len() method returns the number of characters in a string.
sentence = "Hello, world!"
print(len(sentence)) # 13
2. upper()
Converts all letters of a string to uppercase.
sentence = "hello, world!"
print(sentence.upper()) # HELLO, WORLD!
3. lower()
Converts all letters of a string to lowercase.
sentence = "HELLO, WORLD!"
print(sentence.lower()) # hello, world!
4. startswith() and endswith()
These methods are useful for checking whether a string starts or ends with a given substring.
sentence = "Python is wonderful."
print(sentence.startswith("Python")) # True
print(sentence.endswith("Python")) # False
5. split()
This method splits a string into a list, using a defined separator.
sentence = "Python,Java,C++"
languages = sentence.split(",")
print(languages) # ['Python', 'Java', 'C++']
Conclusion
Strings are fundamental in Python, and understanding the many ways to manipulate them is essential for any programmer. With practice and familiarity with the available methods, working with strings becomes a simple and efficient task.