week2
week2
In [3]:len("abc")
Out[3]: 3
2
Strings - Indexing
Each character in a string has an index or position.
In Python, indexing starts from zero to indicate the first element of a string.
Square brackets are used to get the value at a certain index/position
s = "abc"
s[0] evaluates to 'a'
s[1] evaluates to 'b'
s[2] evaluates to 'c'
s[3] trying to index out of bounds, error
s[-1] evaluates to 'c'
s[-2] evaluates to 'b'
s[-3] evaluates to 'a'
index: 0 1 2 indexing always starts at 0
index: -3 -2 -1 last element always at index -1
3
String Operations – indexing
If the index given is outside the bounds of the string, an error message will be displayed.
s[3]
If negative indexes are given, indexing starts from the end of the string.
s[-1]
Out[5]: 'c'
s[-2]
Out[6]: 'b'
s[-3]
Out[7]: 'a'
s[-4]
IndexError: string index out of range
4
String Operations – slicing
Slicing is used to extract substrings of arbitrary length.
If s is a string, the expression s[start:end] denotes the substring of s that starts at index start
and ends at index (end-1).
Examples:
s = "hello world!"
s[0:len(s)]
Out[8]: 'hello world!'
s[0:len(s)-1]
Out[9]: 'hello world’
s[6]
Out[10]: 'w'
s[6:11]
Out[11]: 'world' 5
String Operations – slicing
6
String Concatenation
String concatenation operator (+) : joins two strings.
name = "ana"
name = 'ana'
greet = 'hi' + name
greeting = 'hi' + " " + name
To join data of other types to string, you must first convert the data to a string using the
str() function.
"a" + 5
TypeError: must be str, not int
"a" + str(5) -> "a5"
7
Practice with strings (text values)
1. Create the string ‘CS125 Spring 2022!’ my_str = 'CS125 Spring 2022!'
SEE: string_practice.py
8
Input from the User – input()
To obtain values from the user the input statement is used.
The input statement always returns the input as a string. To use as numeric values you must cast to the
appropriate type.
Examples:
9
Practice with input()
1. Input the id and name of a student (assume student id has 10 characters always).
info = input('Enter your id and name: ')
2. Store the name and id of the student in separate variables.
stu_id = info[:10]
stu_name = info[11:]
2. Create a string in the format: Name: student ID: student id, and
display. formatted = 'Name: '+stu_name+' Student ID: '+stu_id
print(formatted)
2. Store and display the integer year the student entered Bilkent. Assume first 4
characters of id represents start year. year = int(stu_id[:4])
print('Start year: ',year)
2. Display the year the student will graduate (assume 5 years to graduate).
print('Student will graduate in: ', year + 5)
2. Display the number of years the student has left at the university.
print('Years left: ',year + 5 - 2022)
SEE: input_practice.py 10
Comments
Comments can be used to explain Python code to make the code more readable.
Any statements within a comment are ignored by the interpreter (they won’t be executed)
Comments are for the reader of the program, not the interpreter.
There are two types of comments, single line and multi-line comments.
11