Search⌘ K
AI Features

Introduction to Strings

Explore the fundamentals of Python strings by understanding how to create, access, and manipulate them. Learn about string indexing, slicing, immutability, concatenation, and replication to build a strong foundation in text handling.

What is a string?

A Python string is a collection of Unicode characters. Python strings can be enclosed in single, double, or triple quotes, as shown below:

  • 'BlindSpot'
  • "BlindSpot"
  • '''BlindSpot'''
  • """Blindspot"""
Python 3.8
# simple strings
msg1 = 'Educative'
print(msg1)

If there are characters like ', ", or \ within a string, they can be retained in two ways:

  • Escape them by preceding them with a \.
  • Prepend the string with r, therefore, indicating that it is a raw string.
Python 3.8
msg1 = 'He said, \'Let Us Python.\''
print(msg1) # prints He said, 'Let Us Python.'
msg2 = r"He said, 'Let Us Python.'"
print(msg2) # prints He said, 'Let Us Python.'
file1 = 'C:\\temp\\newfile'
print(file1)
file2 = r'C:\temp\newfile' # raw string - prepend r
print(file2)

Multiline strings can be created in three ways:

  • All but the last line ends with \
  • Enclosed within """some msg""" or '''some msg'''
  • Enclosed statements or messages within small brackets, like this:
    ('one msg'
     'another msg')
    

Let’s try these ways in the code widget below:

Python 3.8
str1 = "Hello \
World"
str2 = '''Hello
World'''
# strings are concatenated properly. ( ) necessary
str3 = ('Hello '
'World')
print(str1)
print(str2)
print(str3)

Accessing string elements

String elements can be accessed using an index value starting with 0. A negative index value is allowed. The last character is considered to be at index -1. Positive and negative indices are shown in the figure below.

Layout of a string in memory
Layout of a string in memory

Here are examples of positive and negative indexing:

Python 3.8
msg = 'Hello'
print(msg[0]) # yields H
print(msg[4]) # yields o
print(msg[-0]) # yields H, -0 is same as 0
print(msg[-1]) # yields o
print(msg[-2]) # yields l
print(msg[-5]) # yields H

A substring can be sliced out of a string using:

  • s[start : end]: It extracts a substring from start to end - 1.
  • s[start :]: It extracts a substring from start to end.
  • s[: end]: It extracts a substring from start to end - 1.
  • s[-start :]: It extracts a substring from -start (included) to end.
  • s[: -end]: It extracts a substring from beginning to -end - 1.

Using too large an index reports an error, but using too large an index while slicing is handled elegantly.

Python 3.8
s = 'Bamboozled'
# extract mboozled
print(s[2:10])
print(s[2:])
print(s[-8:])
# extract Bamboo
print(s[0:6])
print(s[:6])
print(s[-10:-4])
print(s[:-4])
# reverse Bamboozled
print(s[::-1])
print(s[0:10:1])
print(s[0:10:2])
print(s[0:10:3])
print(s[0:10:4])
msg = 'Rafting'
print(msg[3:100]) # prints elements from 't' up to end of string
print(msg[100]) # error since 100th element doesn't exist

String properties

  • Python strings are immutable, and they cannot be changed.
s = 'Hello'
s[0] = 'M' # rejected, attempt to mutate 
s = 'Bye'  # s is a variable, it can change
  • Strings can be concatenated using +.
Python 3.8
msg1 = 'Hello '
msg2 = 'World'
msg3 = msg1 + msg2
print(msg3)
s = 'Bamboozled'
s = s + 'Hype!'
print(s)
s = s[:6] + 'Monger' + s[-1]
print(s)
  • Strings can be replicated during printing. Replication occurs when multiplying a string (or concatenated string) by a number using the * operator.
Python 3.8
print('-' * 50) # prints 50 dashes
  • Determining whether one string is part of another can be done using in, as shown below.
Python 3.8
print('e' in 'Hello') # prints True
print('z' in 'Hello') # print False
print('lo' in 'Hello') # prints True