Python Tutorial | Python Strings



What is String in Python?

A string is a sequence of characters, where each character has a unique position/index. The index of the string starts from 0 to length-1 in the forward direction and -1,-2,-3 …. Length-1 in the backward direction.

A character is simply a symbol. For example, the English language has 26 characters.
In real computer do not deal with characters, they deal with numbers (binary numbers). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0’s and 1’s.

This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encoding used.

In Python, the string is a sequence of Unicode characters. Unicode was introduced to include every character in all languages and bring uniformity in encoding.



How to create a string in Python?

Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.

# all of the following are equivalent
n = ‘Hello how are you’
print(n)

n = “Hello how are you”
print(n)

n = ”’Hello how are you”’
print(n)

# triple quotes string can extend to multiple lines

n = “””Hello, how are you
welcome to Python strings “””
print(n)

Output:

Example:

n = 'Hello how are you'
print(n)

n = "Hello how are you"
print(n)

n = '''Hello how are you'''
print(n)

# triple quotes string can extend to multiple lines

n = """Hello, how are you
           welcome to Python strings """
print(n)

Output:

Hello how are you
Hello how are you
Hello how are you
Hello, how are you
           welcome to Python strings 
>>> 


How to access characters in a string?

We can access individual characters using indexing and a range of characters using slicing. The index starts from 0. Trying to access a character out of index range will raise an IndexError.

The index must be an integer. We can’t use float or other types, this will result in TypeError.

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item, and so on. We can access a range of items in a string by using the slicing operator (colon).

Positive index

Example:1
n=”computer”

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

n=”computer”
print(n)
print(n[0])
print(n[2])
print(n[4])
print(n[5])
print(n[7])

output:

computer
c
m
u
t
r

n="computer"
print(n)
print(n[0])
print(n[2])
print(n[4])
print(n[5])
print(n[7])

Output:

computer
c
m
u
t
r
>>> 

Example:2

n1=”catalyst”

Positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

n1=’catalyst’
print(n1)
print(n1[0])
print(n1[2])
print(n1[4])
print(n1[5])
print(n1[7])

output:

catalyst
c
t
l
y
t

n1='catalyst'
print(n1)
print(n1[0])
print(n1[2])
print(n1[4])
print(n1[5])
print(n1[7])

Output:

catalyst
c
t
l
y
t
>>> 

Negative indexing

Example:3

n=”computer”
Positive index

0

1

2

3

4

5

6

7

c

O

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=”computer”
print(n)
print(n[-0])
print(n[-2])
print(n[-4])
print(n[-5])
print(n[-7])

output:

computer
c
e
u
p
o

n="computer"
print(n)
print(n[-0])
print(n[-2])
print(n[-4])
print(n[-5])
print(n[-7])

Output:

computer
c
e
u
p
o
>>> 

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions