Free Ebook cover Python course with Lambda and API Gateway for backend development

Python course with Lambda and API Gateway for backend development

5

(1)

142 pages

Python Language Fundamentals: String Manipulation in Python

Capítulo 9

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

# 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.

The correct statement is that string slicing in Python is a functionality for extracting a part of a string, with the basic syntax being string[start:stop:step]. String slicing allows you to access a part of the string efficiently and is a crucial part of string manipulation in Python.

Next chapter

Python Language Fundamentals: Data Structures in Python

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