0% found this document useful (0 votes)
6 views13 pages

Computer Programming - Lecture 8

This lecture covers strings in Python, defining them as sequences of characters enclosed in single or double quotes. It includes various string operations such as length, case conversion, concatenation, and methods like startswith, endswith, replace, split, and join. Additionally, it explains string indexing, traversing with loops, and slicing to extract substrings.

Uploaded by

Panda
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)
6 views13 pages

Computer Programming - Lecture 8

This lecture covers strings in Python, defining them as sequences of characters enclosed in single or double quotes. It includes various string operations such as length, case conversion, concatenation, and methods like startswith, endswith, replace, split, and join. Additionally, it explains string indexing, traversing with loops, and slicing to extract substrings.

Uploaded by

Panda
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/ 13

Computer

Programming: Lecture
7
Outline
• Strings
Strings
• A string in Python is a sequence of characters. It can be enclosed by either single or double
quotes, although single quotes are more commonly used. For example, the following are all
strings:

'This is a string'
"This is also a string"
Strings
Strings can contain any character, including letters, numbers, punctuation, and whitespace. They
can also contain special characters, such as escape sequences. For example, the following string
contains an escape sequence that represents a newline character:

print('This is a string\nwith a newline character')

This is a string
with a newline character
String Operations
• len(): Returns the length of the string.
print(len(“name”)) -> 4

• upper(): Converts the string to uppercase.


name = "Abebe"
print(name.upper()) => ABEBE

• lower(): Converts the string to lowercase.


name = "Abebe"
print(name.lower()) => abebe

• +: can be used to concatenate strings


string = “Hello “ + “ world“
print(string) => Hello world
String Operations
• startswith(): Returns True if the string starts with a specified substring
name = "Abel"
print(name.startswith("Ab")) => True

• endswith(): Returns True if the string ends with a specified substring


name = "Abel"
print(name.endswith("el")) => True

• replace(): Replaces all occurrences of a substring with another substring.


mystring = "Hello world"
replaced = mystring.replace("Hello","Hi")
print(replaced) => Hi world
String Operations
• split(): Splits the string into a list of substrings based on a delimiter.
mystring = "Hello world"
mylist = mystring.split(" ")#split by space
print(mylist) =>['Hello', 'world’]

• join(): Joins a list of strings together with a delimiter.


my_list = ["hello","world"]
new_string = "-".join(my_list)#join by hyphen
print(new_string)=>hello-world
Indexing a string
• To index a character we use square brackets [] and use an integer starting from 0 to the length
of the string minus one.
• The first character is indexed by 0.
• The last character is indexed by length of the string minus 1.

name = "Abel"
print(name[0]) => A
print(name[1]) => b
print(name[2]) => e
print(name[3]) => l
Traversing a string with a while loop
i=0 A
name = "Abebe" b
e
while(i < len(name)): b
print(name[i]) e

i=i+1
Traversing a string using a for loop

A
for letter in name: b
print(letter) e
b
e
Slicing
• A substring of a string is obtained by taking a slice.
• We use the colon operator to get a continuous section of the string

Syntax
[start index:end index]
Outputs
A substring from the start index to
the end index minus 1
Example

s = 'Monty Python'
print(s[0:2]) => Mo
Example
print (s[8:]) =>thon
#if a value is not provided the default start index is 0 and the end index is length - 1
print (s[:]) => Monty Python
print(s[:4]) => Mont

You might also like