1. Trang chủ
  2. » Công Nghệ Thông Tin

Algorithms Programming with Python Module 1 – Python basics

30 15 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 335,2 KB

Nội dung

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.

Algorithms & Programming with Python Module – Python basics – Lesson Nguyễn Chí Thức gthuc.nguyen@gmail.com 0986636879 String A string can be: • read from the standard input using the function input() • defined in single or double quotes String Two strings can be concatenated (+), and we can also repeat a string n times multiplying it by integer (*): • s1 = "hello " + "world" • s2 = "#" * 100 String 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')) Cast 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)) Slices A 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 • i can be counted from the end (negative index), starting from -1 Slices – single character S = 'Hello’ e l l o String S H Index S[0] S[1] S[2] S[3] S[4] Index S[-5] S[-4] S[-3] S[-2] S[-1] If the index in the slice S[i] is greater than or equal to len(S), or less than -len(S), the following error is caused IndexError: string index out of range Slices – 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' Slices – 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 Slices – 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 + * d and so on, until and not including the character with index b s = 'abcdefghijklm' print(s[0:10:2]) for i in range(0, 10, 2): print(s[i], end='') 10 .find() and rfind() quote = 'Let it be, let it be, let it be' result = quote.find('let it') print("Substring 'let it':", result) result = quote.find('small') print("Substring 'small ':", result) if (quote.find('be,') != -1): print("Contains substring 'be,'") else: print("Doesn't contain substring") 16 .find() and rfind() Substring 'let it': 11 Substring 'small': -1 Contains substring 'be,' 17 .find() and rfind() quote = 'Do small things with great love' print(quote.find('small things', 10)) print(quote.find('small things', 2)) print(quote.find('o small ', 10, -1)) print(quote.find('things ', 6, 20)) 18 .find() and rfind() -1 -1 19 .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 20 .startswith() and endswith() text = "Python is easy to learn." result = text.startswith('is easy') print(result) result = text.startswith('Python is ') print(result) result = text.startswith('Python is easy to learn.') print(result) 21 .startswith() and endswith() False True True 22 .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) result = text.startswith('program', 7, 18) print(result) 23 .startswith() and endswith() True False True 24 .count() string.count(substring, start= , end= ) • returns the number of occurrences of the substring in the given string 25 .count() string = "Python is awesome, isn't it?" substring = "is" count = string.count(substring) print("The count is:", count) count = string.count(substring, 8, 25) print("The count is:", count) 26 .count() 27 .replace() str.replace(old, new [, count]) • returns a copy of the string where old substring is replaced with the new substring 28 .replace() song = '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' print(song.replace('let', "don't let", 2)) 29 .replace() hurt, hurt heart celd, celd heart Let it be, don't let it be, don't let it be, let it be 30 ... text.startswith( 'Python is easy to learn.') print(result) 21 .startswith() and endswith() False True True 22 .startswith() and endswith() text = "Python programming is easy." result = text.startswith( 'programming. .. character with index b s = 'abcdefghijklm' print(s[0 :10 :2]) for i in range(0, 10 , 2): print(s[i], end='') 10 Slices – substring s = 'abcdefg' print(s [1] ) print(s[ -1] ) print(s [1: 3]) print(s [1: -1] ) print(s[:3])... .find() and rfind() -1 -1 19 .startswith() and endswith() str.startswith(prefix[, start[, end]]) str.endswith(suffix[, start[, end]]) • Returns True if the string starts with the specified prefix

Ngày đăng: 20/10/2022, 14:37