0% found this document useful (0 votes)
47 views10 pages

MS1008-Tutorial 5

This document provides an introduction to strings in computational thinking. It discusses that strings are sequences of characters indicated by single or double quotes. It also covers string indexing and slicing, basic string operations like concatenation and membership checks, string methods like upper(), find(), join(), and more. Examples are given to demonstrate accessing characters in a string, slicing strings, concatenating strings, and using string methods. Problems with solutions are provided to practice using strings and common string operations in Python.

Uploaded by

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

MS1008-Tutorial 5

This document provides an introduction to strings in computational thinking. It discusses that strings are sequences of characters indicated by single or double quotes. It also covers string indexing and slicing, basic string operations like concatenation and membership checks, string methods like upper(), find(), join(), and more. Examples are given to demonstrate accessing characters in a string, slicing strings, concatenating strings, and using string methods. Problems with solutions are provided to practice using strings and common string operations in Python.

Uploaded by

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

Introduction to Computational Thinking

MS1008

Week 5
Accessing Strings
A string is a sequence of characters.
It is indicated using single quote (‘…’) or double quotes (“…”).
Index and slicing:
We can use [ ] to access particular characters in a string.

Put an apostrophe in a string:


“John’s apple" (Use different quotes ) or ‘John\’s apple‘ (Use an escape character)
Strings Operations
Extended Slicing

Basic Operations

Membership Operation
Methods for Strings
A method is a variation on a function.
- It represents a program.
- It has input arguments and output.
Unlike a function, it is applied in the context of a particular object.
- This is indicated by the dot notation invocation.
Each string is itself an object

It will output a new string, which is the same as the string on which it was called, except
all letters will now be in upper case.

Input: a single character


Output: the index of the character (first seen
from left to right)
If the character is not found, –1 is returned

Input: the target string to be joined


Output: the new string where the base joins the target
Problem 1

Given the string "Introduction to Computational Thinking", write an


expression to
i. print the first character
ii. print the last character, assuming you don’t know the string
length
iii. print the last character using len()
iv. print the first word
v. print the 4th to 14th characters.
vi. print the 4th to 14th characters in the reverse order
vii.print every alternate character starting from the second
Solution to Problem 1

Given the string "Introduction to Computational Thinking", write an


expression to
i. print the first character
print(S[0])
ii. print the last character, assuming you don’t know the string length
print(S[-1])
iii. print the last character using len()
print(S[len(S)-1])
iv. print the first word
print(S[0:12])
v. print the 4th to 14th characters
print(S[3:14])
vi. print the 4th to 14th characters in the reverse order
print(S[13:2:-1])
vii. print every alternate character starting from the second
print(S[1::2])
Problem 2

What are printed by the following expressions:


i. print("Nanyang"*3)
ii. print("Nanyang"*3.0)
iii. print("Nanyang" + "Technological" + "University")
iv.print("Nanyang" - "N")
Solution to Problem 2

What are printed by the following expressions:


i. print("Nanyang"*3)
NanyangNanyangNanyang
ii. print("Nanyang"*3.0)
TypeError: can't multiply sequence by non-int of type 'float'
iii.print("Nanyang" + "Technological" + "University")
NanyangTechnologicalUniversity
iv.print("Nanyang" - "N")
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Problem 3 (optional)

Study the Python methods associated with string, and write down the
expressions for doing the following, given the string "it's a beautiful
day." (Not all functions have been covered in class.)
i. Capitalize the first word
ii. Capitalize each word
iii.Convert the whole string to upper case
iv.Remove all the spaces
v. Count the number of a specific character in the string
Solution to Problem 3 (optional)
Study the Python methods associated with string, and write down the
expressions for doing the following, given the string "it's a beautiful
day." (Not all functions have been covered in class.)
S1 = "it's a beautiful day."
i. Capitalize the first word
S2 = S1.capitalize()
print(S2)
ii. Capitalize each word
S2 = S1.title()
print(S2)
iii.Convert the whole string to upper case
S2 = S1.upper()
print(S2)
iv.Remove all the spaces
S2 = S1.replace(" ", "")
print(S2)
v. Count the number of a specific character in the string
print(S1.count("a"))

You might also like