Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Lesson 5 String Operations - Part 1

This document discusses string operations in Python. It defines strings as sequences of characters enclosed in single or double quotes. Strings can contain letters, numbers, and special characters. Numeric and string data types are different - strings cannot be used for mathematical operations. The document covers string formatting using escape sequences, multi-line strings, and placeholders. It also introduces several built-in string methods like lower(), upper(), split(), and join(). Examples are provided to demonstrate string indexing, formatting, and basic operations.
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)
35 views

Lesson 5 String Operations - Part 1

This document discusses string operations in Python. It defines strings as sequences of characters enclosed in single or double quotes. Strings can contain letters, numbers, and special characters. Numeric and string data types are different - strings cannot be used for mathematical operations. The document covers string formatting using escape sequences, multi-line strings, and placeholders. It also introduces several built-in string methods like lower(), upper(), split(), and join(). Examples are provided to demonstrate string indexing, formatting, and basic operations.
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/ 15

Welcome to Python Level 1

Lesson 5

String Operations (Part 1)


Key Concepts Breakdown

● What is String?
● Difference between String and numeric data
● String formatting methods
● Basic String operations
What is String?
String is a sequence of characters which includes:
Coding time!!
- letters (A-Z, a-z) Assign your name (in
string) to variable “name”
- numbers (0-9)
- other special characters (@#!$&?)
enclosed by single-quotes (‘ ‘) or double-quotes (“ “). Example of Strings

“Jonathan”
Which of your personal
informations are in String “Python is the best”
data types?
“2022 this year“
”pyth0n@#!$&_pa$$word”
Numeric Data VS. String Data
Operations Numeric (int or float) String

15 + 16 31 “1516”
“15” + “16”

15 * 3 45 “151515”
“15” * 3

Interpret as
Students need to String Concatenation
differentiate which data
types to use
Basic Mathematical Operation
(Add (+), Multiply (*))
String Formatting
In Python, we have different methods to format the output for display
purpose as below:

1. Using escape sequence


2. Using multiple-line String with (‘’’)
3. Using placeholder - int (%d), float (%f), string (%s)

For float, you may use


%.2f to restrict the
output as 2 decimal points
String Formatting - Escape Sequence
Using escape sequence, \n within the double-quotes (“ ”)

Python Syntax

Example Output
String Formatting - Multi-line Strings
Using multi-line strings, enclosed with triple single-quote (‘’’ ‘’’)

Python Syntax

Example Output
String Formatting - Placeholder
Using placeholder, int (%d), float (%f), string (%s)

Python Syntax

Example Output
String Operations
String data type in Python comes with many built-in functions as table below:
String Operations - How to apply?
Coding time!
Type the code out in editor
and fill in the blanks

??
??

?? ??

Display result
Question 1:

Write a Python program to create a new string from an


input string’s first, middle and last character.
Hint:
1. String consists of many characters, each character Example Output
can be referenced by index number [ ].
2. You have to use len(string) to find the last index
number.
Question 1 Hint

Index = 0
Index = 6

If you want to find “h”, you have to use the code


Question 1 Reference Code

Find the first


character using
index 0

?? Find the length


of the string
??

??
Use the first char +
mid char + last char
using index
Question 2:

Write a Python program to display the first character in


uppercase and all the other character from user’s input
to lowercase.
Hint:
1. Take input from users
2. Change the string to lowercase
3. Change only first character to uppercase Example Output
Question 2 Hint
Take input from user and display it out
- Use variable = input() and print() statement

Change the variable all characters to lower case


??
- Use variable.lower()

Change first char to upper case and assign to new variable


??
- Use variable[0].upper()

String concatenation → add the first char to remaining char using


variable[1:], start from index = 1 (second char)

You might also like