0% found this document useful (0 votes)
19 views15 pages

Python String Manipulation Basics

The document provides an overview of string manipulation in Python, highlighting that strings are immutable and can be created using single or double quotes. It explains various string operations such as indexing, slicing, and common methods like lower(), upper(), and replace(). Additionally, it introduces tuples, emphasizing their immutability and methods for accessing and manipulating tuple elements.
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)
19 views15 pages

Python String Manipulation Basics

The document provides an overview of string manipulation in Python, highlighting that strings are immutable and can be created using single or double quotes. It explains various string operations such as indexing, slicing, and common methods like lower(), upper(), and replace(). Additionally, it introduces tuples, emphasizing their immutability and methods for accessing and manipulating tuple elements.
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

UNIT-2

STRING MANIPULATION
String manipulation in Python involves modifying, analyzing, or processing string data
types. Python strings are immutable, meaning that once created, they cannot be changed
directly; any operation that appears to modify a string actually creates a new string.

In Python, a string is a sequence of characters. For example, "hello" is a string containing a


sequence of characters 'h', 'e', 'l', 'l', and 'o'.
We use single quotes or double quotes to represent a string in Python. For example,

# create a string using double quotes


string1 = "Python programming"

# create a string using single quotes


string1 = 'Python programming'

Here, we have created a string variable named string1. The variable is initialized with the
string "Python Programming".
Example: Python String
# create string type variables

name = "Python"
print(name)

message = "I love Python."


print(message)
Output

Python
I love Python.

In the above example, we have created string-type variables: name and message with
values "Python" and "I love Python" respectively.
Here, we have used double quotes to represent strings, but we can use single quotes too.
Access String Characters in Python-We can access the characters in a string in three
ways.

 Indexing: One way is to treat strings as a list and use index values. For example,
greet = 'hello'

# access 1st index element


print(greet[1]) # "e"
 Negative Indexing: Similar to a list, Python allows negative indexing for its strings. For
example,
greet = 'hello'

# access 4th last element


print(greet[-4]) # "e"
 Slicing: Access a range of characters in a string by using the slicing operator colon :. For
example,
greet = 'Hello'

# access character from 1st index to 3rd index


print(greet[1:4]) # "ell"

Note: If we try to access an index out of the range or use numbers other than an integer, we will
get errors.

Python Strings are Immutable

In Python, strings are immutable. That means the characters of a string cannot be changed. For
example,

message = 'Hola Amigos'


message[0] = 'H'
print(message)
Output

TypeError: 'str' object does not support item assignment


However, we can assign the variable name to a new string. For example,

message = 'Hola Amigos'

# assign new string to message variable


message = 'Hello Friends'

print(message); # prints "Hello Friends"

Python Multiline String-We can also create a multiline string in Python. For this, we use triple
double quotes """ or triple single quotes '''. For example,

# multiline string
message = """
Never gonna give you up
Never gonna let you down
"""

print(message)
Output

Never gonna give you up


Never gonna let you down

In the above example, anything inside the enclosing triple quotes is one multiline string.

Basic Operations-Many operations can be performed with strings, which makes it one of the
most used data types in Python.
1. Compare Two StringsWe use the == operator to compare two strings. If two strings are
equal, the operator returns True. Otherwise, it returns False. For example,

str1 = "Hello, world!"


str2 = "I love Swift."
str3 = "Hello, world!"

# compare str1 and str2


print(str1 == str2)
# compare str1 and str3
print(str1 == str3)
Output

False
True

In the above example,

1. str1 and str2 are not equal. Hence, the result is False.
2. str1 and str3 are equal. Hence, the result is True.

2. Join Two or More Strings-In Python, we can join (concatenate) two or more strings using

the + operator.

greet = "Hello, "


name = "Jack"

# using + operator
result = greet + name
print(result)

# Output: Hello, Jack


In the above example, we have used the + operator to join two strings: greet and name.

Iterate Through a Python String

We can iterate through a string using a for loop. For example,


greet = 'Hello'

# iterating through greet string


for letter in greet:
print(letter)
Output

H
e
l
l
o

Python String Length

In Python, we use the len() method to find the length of a string. For example,
greet = 'Hello'

# count length of greet string


print(len(greet))

# Output: 5

String Membership Test

We can test if a substring exists within a string or not, using the keyword in.
print('a' in 'program') # True
print('at' not in 'battle') # False

String slices
String slicing in Python allows the extraction of a portion of a string, creating a new
substring. This is achieved using the slice operator [start:end:step].
 start:
The index where the slice begins (inclusive). If omitted, it defaults to the beginning of the
string (index 0).
 end:
The index where the slice ends (exclusive). The character at this index is not included. If
omitted, it defaults to the end of the string.
 step:
The interval at which characters are extracted. If omitted, it defaults to 1 (extracting every
character). A positive step extracts characters from left to right, while a negative step reverses
the extraction direction.
Examples:
Python
my_string = "Hello, Python!"

# Basic slicing: from index 0 to (but not including) index 5


print(my_string[0:5]) # Output: Hello
# Omitting 'start': starts from the beginning
print(my_string[:5]) # Output: Hello

# Omitting 'end': goes to the end


print(my_string[7:]) # Output: Python!

# Using negative indices: from the 7th character from the end to the end
print(my_string[-7:]) # Output: Python!

# Slicing with a step: extract every other character


print(my_string[::2]) # Output: Hlo yhn

# Reversing a string using a negative step


print(my_string[::-1]) # Output: !nohtyP ,olleH
Important Note: Strings in Python are immutable. String slicing creates a new string; it does not
modify the original string in place.

Python String Methods

Python has a set of built-in methods that you can use on strings.

Note: All string methods returns new values. They do not change the original string.

Method Description

capitalize() Converts the first character to upper case

casefold() Converts string into lower case

center() Returns a centered string

count() Returns the number of times a specified value occurs in a string


encode() Returns an encoded version of the string

endswith() Returns true if the string ends with the specified value

expandtabs() Sets the tab size of the string

find() Searches the string for a specified value and returns the position of
where it was found

format() Formats specified values in a string

format_map() Formats specified values from a dictionary in a string

index() Searches the string for a specified value and returns the position of
where it was found

isalnum() Returns True if all characters in the string are alphanumeric

isalpha() Returns True if all characters in the string are in the alphabet

isascii() Returns True if all characters in the string are ascii characters

isdecimal() Returns True if all characters in the string are decimals


isdigit() Returns True if all characters in the string are digits

isidentifier() Returns True if the string is an identifier

islower() Returns True if all characters in the string are lower case

isnumeric() Returns True if all characters in the string are numeric

isprintable() Returns True if all characters in the string are printable

isspace() Returns True if all characters in the string are whitespaces

istitle() Returns True if the string follows the rules of a title

isupper() Returns True if all characters in the string are upper case

join() Converts the elements of an iterable into a string

ljust() Returns a left justified version of the string

lower() Converts a string into lower case


lstrip() Returns a left trim version of the string

maketrans() Returns a translation table to be used in translations

partition() Returns a tuple where the string is parted into three parts

replace() Returns a string where a specified value is replaced with a specified


value

rfind() Searches the string for a specified value and returns the last position of
where it was found

rindex() Searches the string for a specified value and returns the last position of
where it was found

rjust() Returns a right justified version of the string

rpartition() Returns a tuple where the string is parted into three parts

rsplit() Splits the string at the specified separator, and returns a list

rstrip() Returns a right trim version of the string


split() Splits the string at the specified separator, and returns a list

splitlines() Splits the string at line breaks and returns a list

startswith() Returns true if the string starts with the specified value

strip() Returns a trimmed version of the string

swapcase() Swaps cases, lower case becomes upper case and vice versa

title() Converts the first character of each word to upper case

translate() Returns a translated string

upper() Converts a string into upper case

zfill() Fills the string with a specified number of 0 values at the beginning

Note: All string methods returns new values. They do not change the original string.

Common String Methods with Examples:


 lower() and upper(): Convert the string to lowercase or uppercase.
text = "Hello World"
print([Link]()) # Output: hello world
print([Link]()) # Output: HELLO WORLD
 capitalize(): Capitalizes the first letter of the string and converts the rest to lowercase.
text = "hello world"
print([Link]()) # Output: Hello world

 title(): Converts the first letter of each word to uppercase.


text = "hello world"
print([Link]()) # Output: Hello World
 strip(), lstrip(), rstrip(): Remove leading/trailing whitespace (or specified characters).
text = " Hello World "
print([Link]()) # Output: Hello World
 find() and index(): Find the first occurrence of a substring. index() raises an error if not found,
while find() returns -1.
text = "Hello World"
print([Link]("World")) # Output: 6
 replace(): Replaces all occurrences of a substring with another.
text = "Hello World"
print([Link]("World", "Python")) # Output: Hello Python
 split(): Splits the string into a list of substrings based on a delimiter.
text = "apple,banana,cherry"
print([Link](",")) # Output: ['apple', 'banana', 'cherry']
 join(): Joins elements of an iterable (e.g., a list of strings) into a single string using the string as a
separator.
words = ["Hello", "Python"]
print(" ".join(words)) # Output: Hello Python

 startswith() and endswith(): Check if the string starts or ends with a specified prefix/suffix.
text = "Python Programming"
print([Link]("Python")) # Output: True
print([Link]("ming")) # Output: True
 isdigit(), isalpha(), isalnum(): Check if the string contains only digits, alphabetic characters, or
alphanumeric characters respectively.
num_str = "123"
print(num_str.isdigit()) # Output: True

list
A tuple in Python is an ordered collection of items, similar to a list, but with a key distinction:
immutability. This means that once a tuple is created, its elements cannot be changed, added, or
removed.
Here's an introduction to Python tuples:
1. Definition and Creation: Tuples are defined by enclosing comma-separated items within
parentheses () and Example.
Python
my_tuple = (1, "hello", 3.14, True)

 A tuple with a single item requires a trailing comma:


Python
single_item_tuple = ("apple",)

2. Key Characteristics:
 Ordered:
Items in a tuple maintain a defined order, which means you can access them by their index
(starting from 0).
 Immutable:
This is the defining characteristic. Once created, you cannot modify the contents of a
tuple. This makes them suitable for storing data that should remain constant, like configuration
settings or database records.
 Heterogeneous:
Tuples can store items of different data types within the same tuple (e.g., integers, strings,
booleans, even other lists or tuples).
 Allows Duplicates:
Tuples can contain multiple occurrences of the same value.
Tuple elements in Python can be accessed using indexing and slicing, similar to lists.
Accessing tuples
Tuple elements in Python can be accessed using indexing and slicing, similar to lists.
1. Indexing:
 Positive Indexing: Access individual elements using their position, starting from 0 for the first
element.
Python
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[0]) # Output: apple
print(my_tuple[1]) # Output: banana
 Negative Indexing: Access elements from the end of the tuple, starting from -1 for the last
element.
Python
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[-1]) # Output: cherry
print(my_tuple[-2]) # Output: banana

2. Slicing:
 Range of Elements: Access a subsequence of elements using the slicing operator : within square
brackets []. The syntax is [start:end:step].
o start: The index of the first element to include (inclusive). If omitted, it defaults to 0.
o end: The index of the element to stop before (exclusive). If omitted, it defaults to the end of the
tuple.
o step: The increment between elements (optional, defaults to 1).
Python
my_tuple = ("p", "r", "o", "g", "r", "a", "m", "i", "z")
print(my_tuple[1:4]) # Output: ('r', 'o', 'g')
print(my_tuple[:3]) # Output: ('p', 'r', 'o')
print(my_tuple[6:]) # Output: ('m', 'i', 'z')
print(my_tuple[::2]) # Output: ('p', 'o', 'r', 'm', 'z') (every other element)
print(my_tuple[::-1]) # Output: ('z', 'i', 'm', 'a', 'r', 'g', 'o', 'r', 'p') (reversed tuple)

3. Accessing Elements in Nested Tuples:


 For tuples containing other tuples (nested tuples), you can use nested indexing to access elements
within the inner tuples.
Python
nested_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(nested_tuple[2][1]) # Output: 2 (accesses the second element of the third inner tuple).

Tuple Operations:
 Indexing: Individual elements can be accessed using their index, starting from 0 for the first
element. Negative indices access elements from the end (e.g., -1 for the last element).
Python
my_tuple = (10, 20, 30, 40)
print(my_tuple[0]) # Output: 10
print(my_tuple[-1]) # Output: 40
 Slicing: A range of elements can be extracted using slicing, specifying a start and end index
(exclusive).
Python
my_tuple = (10, 20, 30, 40, 50)
print(my_tuple[1:4]) # Output: (20, 30, 40)

 Concatenation: Two or more tuples can be combined using the + operator to create a new tuple.
Python
tuple1 = (1, 2)
tuple2 = (3, 4)
new_tuple = tuple1 + tuple2
print(new_tuple) # Output: (1, 2, 3, 4)
 Repetition: A tuple can be repeated a specified number of times using the * operator.
Python
my_tuple = ('a', 'b')
repeated_tuple = my_tuple * 3
print(repeated_tuple) # Output: ('a', 'b', 'a', 'b', 'a', 'b')
 Membership Testing: The in and not in operators check if an element exists within a tuple.
Python
my_tuple = (1, 2, 3)
print(2 in my_tuple) # Output: True
print(5 not in my_tuple) # Output: True
 Iteration: Elements of a tuple can be iterated over using a for loop.
Python
my_tuple = ('apple', 'banana', 'cherry')
for item in my_tuple:
print(item)

Tuple Methods
Tuples have two specific methods:
 count(value): This method returns the number of times a specified value appears within the tuple.
Python
my_tuple = (1, 2, 2, 3, 4, 2)
count_of_2 = my_tuple.count(2)
print(count_of_2)

Output:
Code
3
 index(value, start, end): This method returns the index of the first occurrence of a
specified value in the tuple. Optional start and end arguments can be used to limit the search
range. If the value is not found, a ValueError is raised.
Python
fruits = ("apple", "banana", "cherry", "banana")
index_of_banana = [Link]("banana")
print(index_of_banana)

Output:
Code
1

Built-in Functions Applicable to Tuples:


Several general-purpose Python functions can be used with tuples:
 len(tuple): Returns the number of elements in the tuple.
 max(tuple): Returns the largest element in the tuple (requires elements to be comparable).
 min(tuple): Returns the smallest element in the tuple (requires elements to be comparable).
 sum(tuple): Returns the sum of all numeric elements in the tuple.
 sorted(tuple): Returns a new list containing all elements from the tuple in sorted order.
 tuple(iterable): Converts an iterable (like a list or string) into a tuple.
 all(iterable): Returns True if all elements in the tuple are true (or if the tuple is
empty), False otherwise.
 any(iterable): Returns True if any element in the tuple is true, False otherwise.

You might also like