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

session_9_string_processing

Lesson 9 covers string processing in Python, detailing string manipulation, accessing characters, and creating substrings. It explains various string functionalities such as length, concatenation, repetition, and includes examples of string methods like count, lower, upper, and replace. The lesson encourages viewers to ask questions in the comments section of the video.

Uploaded by

saralo9687
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)
2 views7 pages

session_9_string_processing

Lesson 9 covers string processing in Python, detailing string manipulation, accessing characters, and creating substrings. It explains various string functionalities such as length, concatenation, repetition, and includes examples of string methods like count, lower, upper, and replace. The lesson encourages viewers to ask questions in the comments section of the video.

Uploaded by

saralo9687
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

Lesson 9: string processing

• String manipulation
• Exercise and Solution (demo)

1
String
• A string is a sequence of letters (called characters).

• In Python, strings start and end with single or double quotes.

• Each string is stored in the computer’s memory as a list of characters.

• Example >>> myString = “GATTACA”

myString

2
Accessing single characters
• You can access individual characters by using indices in square
brackets.
>>> myString = “GATTACA”
>>> myString[0]
‘G’
>>> myString[1]
‘A’
>>> myString[-1]
‘A’ Negative indices start at the end
>>> myString[-2]
of the string and move left.
‘C’
>>> myString[7]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: string index out of range
3
Substrings
• A substring is the part of a string. Python string provides various
methods to create a substring, check if it contains a substring, index of
substring etc.
>>> myString = “GATTACA”
>>> myString[1:3]
‘AT’
>>> myString[:3]
‘GAT’
>>> myString[4:]
‘ACA’
>>> myString[3:5]
‘TA’
>>> myString[:]
‘GATTACA’
4
More string functionality

>>> len(“GATTACA”) ← Length


7
>>> “GAT” + “TACA” ← Concatenation
‘GATTACA’
>>> “A” * 10
← Repeat
‘AAAAAAAAAA
>>> “GAT” in “GATTACA”
True ← Substring test
>>> “AGT” in “GATTACA”
False

5
String methods

>>> dna = “GATTACA”

>>> dna.count("T") ➔2
>>> dna.lower() ➔ 'gattaca'
>>> dna.upper() ➔ 'GATTACA'
>>> dna.replace("G", "U") ➔'UATTACA‘
>>> dna.replace("C", "U") ➔'GATTAUA'
>>> dna.replace("AT", "**") ➔'G**TACA'
>>> dna.startswith("G") ➔True
>>> dna.startswith("g") ➔False

6
If you have any questions, write it on
a comment section of this video

You might also like