CS106-Week-04
CS106-Week-04
Introduction to
Computer
Programming
By: Dr. Abdus Salam
Abasyn University Peshawar
04
Week
CS106-Introduction to Computer Programming
07
Lecture
CS106-Introduction to Computer Programming
Topics to be covered
02 String Method/Functions
Understanding the use of built-in Python String methods and
functions to perform different tasks. Index and Slice Strings.
Using the input()
Function
● The input() function allows user input. It takes a string
argument that it uses to prompt the user for this text.
● Syntax
input(prompt)
Output:
Using the input()
Function
# Example 2
# Demonstrates getting user input
name = input("Hello. What's your name? ")
sal = input(“And What's your Salary? ")
print("Hi, my name is ", name ," and I am getting " , sal , “ salary")
Output:
String Method/Functions
Method Description
capitalize() Converts the first character to upper case
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it was found
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
String Method/Functions
Method Description
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
zfill() Fills the string with a specified number of 0 values at the beginning
Example
s
Let’s see some examples to understand the
Python String Methods
Using String Functions
# Example 1
# The upper() method returns the string in upper case:
>>> a = "Hello, World!"
>>> print(a.upper())
HELLO, WORLD!
# Example 2
# The lower() method returns the string in lower case:
>>> a = "Hello, World!"
>>> print(a.lower())
hello, world!
Using String Functions
# Example 3
# The replace() method replaces a string with another string:
>>> a = "Hello, World!"
>>> print(a.replace("H", "J"))
Jello, World!
# Example 4
# The split() method returns a list where the text between the
specified separator becomes the list items.
>>> a = "Hello, World!"
>>> print(a.split(","))
['Hello', ' World!']
Using String Functions
# Example 5:
name = input("Hello. What's your name? ")
sal = input(“And What's your Salary? ")
print("Hi, my name is ", name.upper() ," and I am getting " ,sal, “
salary")
Practice & Homework
01 String Slicing
Python String slicing is a way of creating a sub-string from a
given string.
02 String Format
String formatting allows you to create dynamic strings by combining
variables and values.
String Slicing
● In Python, a string is an ordered sequence of Unicode characters. Each
character in the string has a unique index in the sequence.
● The index starts with 0. First character in the string has its positional index
0. The index keeps incrementing towards the end of string.
● Python slicing can be done in two ways:
● Python has several tools for string interpolation that support many formatting
features. In modern Python, you’ll use f-strings or the .format() method most of the
time. However, you’ll see the modulo operator (%) being used in legacy code.
●For example:
# string interpolation using an f-string:
>>> name = ‘Abasyn University’
>>> print(‘Welcome to ' + x)
Welcome to Abasyn University
Using str.format() to Format
Strings
●The .format() method to format values during string interpolation.
●For example:
# string interpolation using .format():
>>> sum_template = "{0} plus {1} is {2}"
>>> a = 5
>>> b = 10
>>> print(sum_template.format(a, b, a + b))
5 plus 10 is 15
Formatting Strings With %
●Strings in Python have a built-in operation that
can be accessed with the modulo operator
(%). This operator does positional and named
string interpolation.
— Linus Torvalds