Python From Beginner to Advanced

0% completed

Previous
Next
Introduction to Strings in Python

Strings in Python are sequences of characters and are among the most widely used data types.

Strings are immutable, which means that once a string is created, its characters cannot be modified directly. This property ensures that strings can be passed around safely in a program without altering the original data. Python treats single characters as strings of length one, making it consistent in handling textual data. Various operations such as slicing, concatenating, and formatting are available, allowing for robust manipulation of text data.

Creating a String

Strings in Python can be created using single quotes, double quotes, or triple quotes for multiline strings. This flexibility allows for easy handling of text data in various scenarios:

  • Single quotes: Ideal for most strings, and helps in avoiding issues with special characters like the double quote.
  • Double quotes: Useful when the string itself contains single quotes/apostrophes.
  • Triple quotes: Best for creating multiline strings and strings that contain both single and double quotes.
Image

Example

In the following example, we will define strings using different types of quotes and print them:

Python3
Python3

. . . .

Explanation:

  • single_quote_string is encapsulated within single quotes, suitable for simple strings.
  • double_quote_string uses double quotes to incorporate an apostrophe, demonstrating the utility of double quotes in such cases.
  • multiline_string uses triple quotes to span multiple lines and include both single and double quotes, showing how triple quotes can be useful for complex strings.

Accessing String Characters and Understanding String Indexing

String indexing in Python is a powerful feature that allows you to access individual characters in a string. Python supports both positive and negative indexing.

  • Positive Indexing: Begins at the start of the string with the index 0 for the first character, 1 for the second, and so on.
  • Negative Indexing: Starts at the end of the string with the index -1 for the last character, -2 for the second-last, and continues similarly towards the start of the string.
Image

Example

Here, we demonstrate how to access characters from a given string using both positive and negative indexing, and we'll print the outputs to illustrate:

Python3
Python3

. . . .

Explanation:

  • phrase[7]: Accesses the eighth character using positive indexing, which is 'p' in "Python programming is fun!".
  • phrase[-4]: Accesses the fourth character from the end using negative indexing, which is 'f' in the same string.
  • first_char = phrase[0]: Retrieves 'P', the very first character, demonstrating positive indexing starting at zero.
  • last_char = phrase[-1]: Retrieves '!', the last character, showcasing negative indexing starting from the end.

In the next lesson, we will learn to use stirng operators.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next