You are on page 1of 3

Grade 09 - Topic 02 – Programming and Development

NOTE - PART III

String Manipulation
1. String Concatenation.
2. Accessing a single character in a certain position in a string.
3. Slicing.

String Concatenation
❖ String concatenation is the process of merging multiple string values into one string value.
❖ Example:

Code Output
firstName = "Sudam" [Keep this space]
lastName = "Perera"

fullName01 = firstName + lastName


fullName02 = firstName + " " + lastName

print(fullName01)
print(fullName02)

Accessing a single character in a certain position in a string


❖ All characters in a string are stored just like in an array.
❖ Example:

Code How the string value is stored in memory


name01 = "Sudam Perera"

Wednesday, October 19, 2022 Page 1 of 3


Grade 09 - Topic 02 – Programming and Development
NOTE - PART III

❖ So, to access a single value in a string we can use the index number of the position.
❖ Example:

Code Output
name01 = "Sudam Perera" [Keep this space]
print(name01[2])

Slicing a string
❖ Slicing is getting a subset (a part) of a string using starting and ending index numbers.
❖ Example 01:

❖ print( name01[1:7] ) → Index 1 to index (7-1) → udam P


❖ print( name01[1:] ) → Index 1 to the end → udam Perera
❖ print( name01[:7] ) → Beginning to index (7-1) → Sudam P

Wednesday, October 19, 2022 Page 2 of 3


Grade 09 - Topic 02 – Programming and Development
NOTE - PART III

Logical Operators in Python


❖ Logical operators are used to combine results of multiple logical conditions together and get a
single logical result.
1. not Operator.
2. and Operator.
3. or Operator.

not Operator
❖ Negates the logical result.

Result not(Result)
True False
False True

and Operator
❖ Both conditions have to be true in order to final result be true.

Logical Condition 01 Logical Condition 02 Final Result


True True True
True False False
False True False
False False False

or Operator
❖ At least one condition must be true in order to final result be true.

Logical Condition 01 Logical Condition 02 Final Result


True True True
True False True
False True True
False False False

Wednesday, October 19, 2022 Page 3 of 3

You might also like