Browse Courses

Strings

This document explains Python strings, including indexing, slicing concatenation, replication, immutability, escape sequences, and string methods for manipulating character data.

This document covers Python strings, including indexing, slicing, concatenation, replication, immutability, escape sequences, and string methods for manipulating character data.


Introduction

Strings in Python are sequences of characters enclosed in quotes. They can contain letters, digits, spaces, and special characters.


String Indexing and Slicing

Strings are ordered sequences, and each character can be accessed by its index.

  • Positive indexing starts from 0.
  • Negative indexing starts from -1 (last character).

Example:

1text = "Michael Jackson"
2first_char = text[0]      # 'M'
3sixth_char = text[6]      # 'l'
4thirteenth_char = text[13] # 'o'
5last_char = text[-1]      # 'n'

Slicing and strides allow selection of multiple characters:

1# Select every second character
2text[::2]
3# Select every second character up to index 4
4text[:4:2]

String Operations

  • Use len() to get the length of a string.
  • Concatenate strings with +.
  • Replicate strings with *.

Example:

1# Concatenation
2new_text = "Michael " + "Jackson"
3# Replication
4repeat_text = "Hi! " * 3  # 'Hi! Hi! Hi! '

Strings are immutable; their values cannot be changed, but new strings can be created by combining or modifying existing ones.


Escape Sequences

Escape sequences allow special formatting:

  • \n for new line
  • \t for tab
  • \\ for backslash
  • Prefix with r for raw strings

Example:

1print("Line1\nLine2")  # Output on two lines
2print("Tab\tSpace")    # Output with tab
3print("C:\\Users\\Name") # Output with backslashes
4print(r"C:\Users\Name") # Raw string

String Methods

Strings have methods for manipulation:

  • upper(): Converts to uppercase
  • replace(): Replaces a substring
  • find(): Finds a substring index

Example:

1A = "Michael Jackson"
2B = A.upper()           # 'MICHAEL JACKSON'
3C = A.replace("Michael", "Janet") # 'Janet Jackson'
4index = A.find("Jack")  # 8
5not_found = A.find("XYZ") # -1

Conclusion

Python strings are powerful tools for handling character data. Indexing, slicing, concatenation, replication, escape sequences, and methods enable flexible and efficient string manipulation.


FAQ

A string in Python is an ordered sequence of characters enclosed in quotes, which can include letters, digits, spaces, and special characters.

Negative indexing allows access to characters from the end of the string, with -1 referring to the last character.

  1. Concatenation with +
  2. Replication with *
  3. Slicing with [start:end]
  4. Subtraction with -
(4) Subtraction is not a valid operation for strings in Python.

Strings are immutable in Python, so their values cannot be changed directly. Instead, new strings must be created.

ConceptDescription
A. Indexing1. Accessing characters by position
B. Slicing2. Selecting a range of characters
C. Escape sequence3. Formatting special characters
D. Method4. Function that manipulates a string
A-1, B-2, C-3, D-4.

The method upper() returns a new string with all characters in uppercase, leaving the original string unchanged.

True. String methods return new strings and do not modify the original.

Ensure the substring exists in the string, as find() returns -1 if not found.

The escape sequence \t is used to insert a tab in a string.

Replicating a string with * creates a new string consisting of multiple copies of the original.

Raw strings treat backslashes as literal characters, preventing escape sequence interpretation.