String Handling
In computer programming,
• A string is a sequence of characters
• It is used to store text.
0 1 2 3 4 5 6 Indices
• Pseudocode string starts at position 1 on the other
hand Python strings start at position 0
Some examples of strings are:
• It contains: (A-Z, 0-9, special characters such as “csekj@gmail.com”
“Asad123”
!,@,#,$,%)
“Cgsgottalent”
“Programming”
“Henry”
“Hi Henry”
String Handling(LENGTH)
String manipulation methods are usually provided in
programming languages by library routines.
Most programming languages support many different string
handling methods.
len()
Length : To find the number of characters in the string.
• For example: “Computer Science” has 16 characters as
spaces are also counted as a character.
Python code –
Pseudocode -
len(“Computer Science”)
LENGTH(“Computer Science”) returns 16
MyString = “Science”
MyString = “Science”
len(MyString)
LENGTH(MyString) returns 8
String Handling(SUBSTRING)
String handling substring indicates -
• To extract a part of a string.
• SUBSTRING(“value”, “start index”, ”no of characters”)
• For example: “Science” could be extracted from
“Computer Science”
Pseudocode -
SUBSTRING(“Computer Science”, 10, 7) Python code – “Computer Science”[9:16]
MyString “Computer Science” MyString[9:16]
SUBSTRING(MyString, 10, 7)
Both of the above function returns “Science” as a part of a given string “Computer Science”
String Handling(UPPER)
String handling upper indicates -
• To convert all the characters in a string
to uppercase.
upper()
• For example: the string “Science” would
become “SCIENCE”
Pseudocode - Python code –
UCASE(“Science”) “Science”.upper()
MyString = “Science” MyString = “Science”
UCASE(MyString) MyString.upper()
String Handling(LOWER)
String handling lower indicates -
• To convert all the characters in a string
to lowercase.
lower()
• For example: the string “Science” would
become “science”
Pseudocode - Python code –
LCASE(“Science”) “Science”.lower()
MyString = “Science” MyString = “Science”
LCASE(MyString) MyString.lower()