PowerPoint Presentation Algorithms Programming with Python Module 1 – Python basics – Lesson 6 Nguyễn Chí Thức gthucString 2 A string can b.PowerPoint Presentation Algorithms Programming with Python Module 1 – Python basics – Lesson 6 Nguyễn Chí Thức gthucString 2 A string can b.
Trang 1Algorithms & Programming with
Python
Module 1 – Python basics – Lesson 6
Nguyễn Chí Thức gthuc.nguyen@gmail.com
0986636879
Trang 2A string can be:
• read from the standard input using the
function input()
• defined in single or double quotes
Trang 4String length
A string in Python is a sequence of characters
The function len(some_string) returns how
many characters there are in a string:
print(len('abcdefghijklmnopqrstuvwxyz'))
Trang 5Cast to string
Every object in Python can be converted to
string using the function str(some_object)
So we can convert numbers to strings:
s = str(2 ** 10)
print(s)
print(len(s))
Trang 6A slice gives from the given string one character
or some fragment: substring or subsequence
Single character:
• S[i] gives ith character of the string
• i (index) is count starting from 0
• i can be counted from the end (negative
index), starting from -1
Trang 7Slices – single character
Trang 8Slices – substring
S[a:b] returns the substring of length b - a, starting with the character at index a and lasting until the character at index b, not including the
last one
S = 'Hello’
S[1:4] == 'ell' == S[-4:-1]
S[1:-1] == 'ell'
Trang 9Slices – substring
• If you omit the second parameter (but
preserve the colon), then the slice goes to the
end of string: s[1:]
• If you omit the first parameter, then Python
takes the slice from the beginning of the
string: s[:-1]
• S[:] matches the string S itself.
Trang 10Slices – subsequence
• S[a:b:d], the third parameter specifies the
step, same as for function range()
• only the characters with the following index
are taken: a, a + d, a + 2 * d and so
on, until and not including the character with
Trang 12Immutability of string
• In Python strings are immutable
• Any slice of a string creates a new string and
never modifies the original one
Trang 13.upper() and lower()
Trang 14.lstrip(), rstrip() and strip()
Trang 15.find() and rfind()
str.find(sub[, start[, end]] )
• If substring exists inside the string, it returns
the lowest index where substring is found
• If substring doesn't exist inside the string, it
returns -1
Trang 16.find() and rfind()
quote = 'Let it be, let it be, let it be'
result = quote.find('let it')
print("Substring 'let it':", result)
Trang 17.find() and rfind()
Substring 'let it': 11
Substring 'small': -1
Contains substring 'be,'
Trang 18.find() and rfind()
quote = 'Do small things with great
Trang 19.find() and rfind()
-1
3
-1
9
Trang 20.startswith() and endswith()
str.startswith(prefix[, start[, end]])
str.endswith(suffix[, start[, end]])
• Returns True if the string starts with the
specified prefix.
• Returns False if the string doesn't start with
the specified prefix.
Trang 21.startswith() and endswith()
text = "Python is easy to learn."
result = text.startswith('is easy')
Trang 22.startswith() and endswith()
False
True
True
Trang 23.startswith() and endswith()
text = "Python programming is easy."
result = text.startswith('programming is', 7)
print(result)
result = text.startswith('programming is', 7, 18)
print(result)
Trang 24.startswith() and endswith()
True
False
True
Trang 25string.count(substring, start= , end= )
• returns the number of occurrences of the
substring in the given string
Trang 272
1
Trang 28str.replace(old, new [, count])
• returns a copy of the string
where old substring is replaced with
the new substring
Trang 29song = 'cold, cold heart'
print (song.replace('cold', 'hurt’))
replaced_song = song.replace('o', 'e')
print (replaced_song)
song = 'Let it be, let it be, let it be, let
it be'
Trang 30hurt, hurt heart
celd, celd heart
Let it be, don't let it be, don't let it be, let it be