0% found this document useful (0 votes)
7 views7 pages

Python Strings - Jupyter Notebook

This document provides an overview of Python strings, including their definition, basic operations, and manipulation techniques such as indexing, slicing, and concatenation. It also covers various string methods for transforming and evaluating string content, including upper/lower case conversion, replacement, and searching. The concepts discussed are essential for effectively working with text data in Python.

Uploaded by

gihananjulayt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Python Strings - Jupyter Notebook

This document provides an overview of Python strings, including their definition, basic operations, and manipulation techniques such as indexing, slicing, and concatenation. It also covers various string methods for transforming and evaluating string content, including upper/lower case conversion, replacement, and searching. The concepts discussed are essential for effectively working with text data in Python.

Uploaded by

gihananjulayt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Strings: Operations and

Manipulation
Objectives:
Work with Strings
Perform basic string operations
Manipulate strings using indexing and slicing

What are Strings?

In Python, a string represents a series of characters. A string can be defined using single ( ' )
or double ( " ) quotes. For example:

In [1]: # Using double quotes


"Michael Jackson"

# Using single quotes
'Michael Jackson'

Out[1]: 'Michael Jackson'

Strings with Spaces and Digits

Strings can also contain spaces, digits, and even special characters:

In [2]: # String with digits and spaces


'1 2 3 4 5 6'

# String with special characters
'@#2_#]&*^%$'

Out[2]: '@#2_#]&*^%$'

Printing Strings

You can display a string using the print() function:


In [3]: # Print a string
print("Hello, world!")

Hello, world!

Assigning Strings to Variables

You can assign a string to a variable:

In [4]: name = "Michael Jackson"


print(name)

Michael Jackson

Indexing

Strings in Python are zero-indexed, meaning the first character has an index of 0 , the second
has an index of 1 , and so on.

[Tip]: Because indexing starts at 0, it means the first index is on the index 0.

Accessing Characters via Indexing


In [5]: # Access the first character
print(name[0]) # Output: 'M'

# Access the seventh character
print(name[6]) # Output: 'l'

# Access the fourteenth character
print(name[13]) # Output: 'o'

M
l
o

Negative Indexing

You can also use negative indices to access characters from the end of the string:

In [6]: # Access the last character


print(name[-1]) # Output: 'n'

# Access the first character using negative index
print(name[-15]) # Output: 'M'

n
M

Finding the Length of a String

Use the len() function to find the number of characters in a string:

In [7]: print(len(name)) # Output: 15

15

String Slicing

Slicing allows you to extract a substring from a string using the syntax str[start:end] .
In [8]: # Slice from index 0 to index 3 (not inclusive)
print(name[0:4]) # Output: 'Mich'

# Slice from index 8 to index 11
print(name[8:12]) # Output: 'Jack'

Mich
Jack

String Stride

Stride allows you to skip characters during slicing. The syntax is str[start:end:stride] .

In [9]: # Select every second character


print(name[::2]) # Output: 'McalJcsn'

# Select every second character from index 0 to index 4
print(name[0:5:2]) # Output: 'Mca'

McalJcsn
Mca

Reversing a String

You can reverse a string by using a negative stride:

In [10]: # Reverse the string


print(name[::-1]) # Output: 'noskcaJ leahciM'

noskcaJ leahciM
String Concatenation and Replication
You can concatenate strings using the + operator:

In [11]: statement = name + " is the best"


print(statement) # Output: 'Michael Jackson is the best'

Michael Jackson is the best

You can replicate strings using the * operator:

In [12]: print(3 * "Hello ") # Output: 'Hello Hello Hello '

Hello Hello Hello

String Methods

Python provides a variety of string methods to manipulate strings. Below are a few commonly
used methods:

upper() and lower()

Convert the case of strings:

In [13]: text = "Thriller is the sixth studio album"


print(text.upper()) # Output: 'THRILLER IS THE SIXTH STUDIO ALBUM'
print(text.lower()) # Output: 'thriller is the sixth studio album'

THRILLER IS THE SIXTH STUDIO ALBUM


thriller is the sixth studio album

replace()

Replace parts of a string with another string:

In [14]: text = "Michael Jackson is the best Michael"


print(text.replace('Michael', 'Janet'))
# Output: 'Janet Jackson is the best Janet'

Janet Jackson is the best Janet

find()
Find the index of the first occurrence of a substring. Returns -1 if the substring is not found:

In [15]: print(name.find('el')) # Output: 5


print(name.find('Jack')) # Output: 8

5
8

Boolean Methods: isupper() , islower() , isnumeric() ,


isalnum() , isalpha()
These methods return True or False based on the content of the string:

In [16]: # Check if the string is uppercase


print("HELLO".isupper()) # Output: True

# Check if the string is lowercase
print("hello".islower()) # Output: True

# Check if the string contains only numeric characters
print("12345".isnumeric()) # Output: True

# Check if the string is alphanumeric
print("M234onica".isalnum()) # Output: True

# Check if the string contains only alphabets
print("Monica".isalpha()) # Output: True

True
True
True
True
True

startswith() and endswith()

Check whether a string starts or ends with a specific substring:

In [17]: print(name.startswith("Michael")) # Output: True


print(name.endswith("Jackson")) # Output: True

True
True

Splitting Strings
In [18]: text = 'Python is a fun programming language'
print(text.split(' '))
# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']

['Python', 'is', 'a', 'fun', 'programming', 'language']

You can also split by other characters, like commas:

In [19]: grocery = 'Milk,Chicken,Bread,Butter'


print(grocery.split(','))
# Output: ['Milk', 'Chicken', 'Bread', 'Butter']

['Milk', 'Chicken', 'Bread', 'Butter']

Summary
In this notebook, we covered:

Basic string operations like indexing, slicing, and concatenation


String methods for transforming and evaluating string content
Manipulating strings using functions such as split() , find() , and replace()

These concepts are fundamental for working with text data in Python.

You might also like