Fundamentals of the Python Language: String Manipulation in Python

Python is a high-level programming language popular for its clear syntax and code readability. One of the many features that make Python a preferred choice for software developers is its string manipulation functionality. Strings in Python are sequences of characters and are widely used in many programming contexts.

1. String Creation

In Python, strings can be created by enclosing characters in single quotes ('...') or double quotes ("..."). Python also supports multiline strings, which can be created by enclosing characters within three single quotes ('''...''') or three double quotes ("""...""").

# Example of creating strings
string1 = 'Hello World!'
string2 = "Python is great!"
string3 = '''This is an example
of a string of several
lines in Python.'''

2. Accessing Characters in Strings

Python allows you to access individual characters in a string using indexes. The index of a character is the position of the character in the string. Python supports both positive indexing (starting from the beginning of the string, 0) and negative indexing (starting from the end of the string, -1).

# Example of accessing characters in strings
string = 'Python'
print(string[0]) # Output: P
print(string[-1]) # Output: n

3. String Slice

String slicing in Python is a feature to extract a part of a string. The basic syntax for string slicing is string[start:stop:step].

# Example of string slicing
string = 'Python'
print(string[0:2]) # Output: Py
print(string[::2]) # Output: Pto

4. Operations with Strings

Python offers a variety of operations that can be performed on strings, such as concatenation (+), repetition (*), membership (in), etc. Furthermore, Python also offers many built-in methods for manipulating strings, such as lower(), upper(), split(), replace(), etc.

# Example of operations with strings
string1 = 'Hello,'
string2 = ' World!'
print(string1 + string2) # Output: Hello World!
print(string1 * 3) # Output: Hello,Hello,Hello,
print('M' in string2) # Output: True
print(string2.lower()) # Output: world!
print(string2.upper()) # Output: WORLD!
print(string2.split(' ')) # Output: ['', 'World!']
print(string2.replace('Mundo', 'Python')) # Output: Python!

In summary, string manipulation in Python is an essential skill for any Python developer. With a solid understanding of string operations and methods, you can manipulate text data efficiently and effectively in your Python programs.

Now answer the exercise about the content:

Which of the following statements about string manipulation in Python is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Python Language Fundamentals: Data Structures in Python 10

Next page of the Free Ebook:

Python Language Fundamentals: Data Structures in Python

Estimated reading time: 3 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks