Python Strings


Introduction to Python Strings

Python strings are the collection of characters. Python stores the string as an ordered sequence, and we can access any element in the sequence using index positions.

Python Strings

We can use single quotes or double quotes to create a string. Either we use single quote or double quote, both mean one and the same thing.

String with double quote.

In [1]:
print("Hello")
Hello

String with single quote.

In [2]:
print('String in a single quote')
String in a single quote

This string will results in an error because “I’m” ended the string. To fix this type of error we can use double quotes(“”).

In [3]:
print('I'm trying to create an error')
  File "<ipython-input-3-dc583acc9b1a>", line 1
    print('I'm trying to create an error')
             ^
SyntaxError: invalid syntax

Now we won’t see any error.

In [4]:
print("I'm trying to fix an error")
I'm trying to fix an error

Multiline String

We can create a multiline string using the triple quotes. It can be single triple quotes or double triple quotes. It will also retain the tabs and whitespace.

Double triple quotes.

In [1]:
greeting = """
Hello,
    How are you.

Thanks for visiting.
"""

print(greeting)
Hello,
    How are you.

Thanks for visiting.

Single triple quotes.

In [2]:
greeting = '''
Hello,
    How are you.

Thanks for visiting.
'''

print(greeting)
Hello,
    How are you.

Thanks for visiting.

String Immutability

Strings are immutable, which means once we assign a value to a string variable, we cannot replace a specific single character of the string. We can reassign the whole string, but cannot replace a single character.

In [1]:
name = 'Peter Omith'
print(name[6])
O

Now we want to replace ‘O’ with ‘S’ using its index position. But we cannot do that because of its immutability.

In [2]:
name[6] = 'S'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-766904bcebc5> in <module>
----> 1 name[6] = 'S'

TypeError: 'str' object does not support item assignment

But, we can reassign the whole string as:

In [3]:
name = 'Peter Smith'
print(name)
Peter Smith

Concatenating Strings

Using the + operator, we can concatenate two strings. While we are concatenating the strings with the plus operator, note that it is not adding any white space in between.

In [1]:
first_name = "Logical"
last_name = "Python"
name = first_name + " " + last_name
print(name)
Logical Python

Check if a sub-string exists – Membership Test

To check whether the sub-string exists in the sting or not, we use the in operator. If we use not with in, we can check the vice versa as well. It will check if the string is not present in the string.

Check if a sub-string exists.

In [1]:
name = "Logical Python"
if "Python" in name:
    print("Welcome to Python Tutorial")
else:
    print("Welcome to Java Tutorial")
Welcome to Python Tutorial

Check if a sub-string does not exists.

In [2]:
name = "Logical Python"
if "Python" not in name:
    print("Welcome to Python Tutorial")
else:
    print("Welcome to Java Tutorial")
Welcome to Java Tutorial

String Functions

Python has a few built-in functions that can be used with the Strings. Let us go through them:

Brief of each:

  1. len() – Simply returns the number of characters in the String.
  2. char() – Accepts integer as an input and converts the integer to String Character.
  3. ord() – This accepts a string character as an input and converts it into an integer.

Here are the examples of each of them:

len()

In [1]:
str = "Logical Python"
print(len(str))
14
In [2]:
str = "Logical Python"
print(len(str))
14

ord()

In [3]:
print(ord('A'))
65
In [4]:
print(ord('a'))
97

chr()

In [5]:
print(chr(65))
A
In [6]:
print(chr(97))
a

References

  1. Strings and Character Data in Python
  2. Python Strings
  3. Introduction to Python Strings