Search⌘ K
AI Features

Strings

Explore Python strings by learning how to create, access, and manipulate them using indexing, slicing, concatenation, and built-in methods. Understand string immutability and use the format method to insert variables efficiently.

Strings are one of the most popular and useful data types in Python. A string can be created by enclosing characters in either single quotes (' ') or double quotes (" "). Python treats single and double quotes the same. Strings are used to record text information (for example, a person’s name) as well as an arbitrary collection of bytes (for example, the contents of an image file).

Python 3.5
# creating single quotes string
string_example = 'creating single quotes string'
# creating double quotes string
string_example_2 = "creating double quotes string"
# we have lots of other quotes, let's wrap them in double quotes
string_example_3 = "we have lots of other quotes, let's wrap them in double quotes"
print(string_example)
print(string_example_2)
print(string_example_3)

Some more about strings

  • A string is a sequence (a positionally ordered collection) of other objects or elements.
  • A string maintains a left-to-right order among the contained items.
  • Items in a string are stored and fetched by their relative positions.
  • Strings are immutable in Python. This means they cannot be changed in place after they are created. In other words, immutable objects can never be overwritten. We can’t change a string by assigning it to one of its positions, but we can always build a
...