Bài tập String - Cơ bản
- Viết hàm
to_uppercasechuyển một chuỗi thành chữ HOA.
def to_uppercase(text):
# Code của bạn ở đây
pass
# Test
print(to_uppercase("hello world")) # "HELLO WORLD"-
Viết hàm
to_lowercasechuyển một chuỗi thành chữ thường. -
Viết hàm
capitalize_firstviết hoa chữ cái đầu tiên của chuỗi. -
Viết hàm
count_spacesđếm số khoảng trắng trong một chuỗi.
def count_spaces(text):
# Code của bạn ở đây
pass
# Test
print(count_spaces("Hello World Python")) # 2- Viết hàm
starts_with_vowelkiểm tra xem chuỗi có bắt đầu bằng nguyên âm (a, e, i, o, u) không.
def starts_with_vowel(text):
# Code của bạn ở đây
pass
# Test
print(starts_with_vowel("apple")) # True
print(starts_with_vowel("banana")) # False- Viết hàm
ends_with_extensionkiểm tra xem tên file có đúng extension không.
def ends_with_extension(filename, extension):
# Code của bạn ở đây
pass
# Test
print(ends_with_extension("document.pdf", ".pdf")) # True
print(ends_with_extension("image.jpg", ".png")) # False- Viết hàm
is_all_digitskiểm tra xem chuỗi chỉ chứa chữ số hay không.
def is_all_digits(text):
# Code của bạn ở đây
pass
# Test
print(is_all_digits("12345")) # True
print(is_all_digits("123a5")) # False- Viết hàm
replace_spacesthay thế tất cả khoảng trắng bằng dấu gạch ngang.
def replace_spaces(text):
# Code của bạn ở đây
pass
# Test
print(replace_spaces("Hello World Python"))
# "Hello-World-Python"- Viết hàm
split_wordstách chuỗi thành list các từ.
def split_words(text):
# Code của bạn ở đây
pass
# Test
print(split_words("Hello World Python"))
# ["Hello", "World", "Python"]- Viết hàm
join_wordsnối list các từ thành chuỗi, cách nhau bởi khoảng trắng.
def join_words(words):
# Code của bạn ở đây
pass
# Test
words = ["Hello", "World", "Python"]
print(join_words(words)) # "Hello World Python"- Viết hàm
remove_spacesxoá tất cả khoảng trắng ở đầu và cuối chuỗi.
def remove_spaces(text):
# Code của bạn ở đây
pass
# Test
print(f"'{remove_spaces(' Hello ')}'") # "'Hello'"- Viết hàm
find_positiontìm vị trí đầu tiên của một từ trong chuỗi.
def find_position(text, word):
# Code của bạn ở đây
pass
# Test
print(find_position("Hello World", "World")) # 6
print(find_position("Hello World", "Python")) # -1- Viết hàm
count_wordđếm số lần xuất hiện của một từ trong chuỗi.
def count_word(text, word):
# Code của bạn ở đây
pass
# Test
text = "hello world hello python hello"
print(count_word(text, "hello")) # 3- Viết hàm
format_namesử dụng f-string để tạo câu giới thiệu.
def format_name(name, age):
# Code của bạn ở đây (dùng f-string)
pass
# Test
print(format_name("Alice", 25))
# "My name is Alice and I am 25 years old"- Viết hàm
format_priceformat giá tiền với 2 chữ số thập phân.
def format_price(price):
# Code của bạn ở đây
pass
# Test
print(format_price(1234.5)) # "1234.50"
print(format_price(99.999)) # "100.00"- Viết hàm
format_table_rowformat một hàng trong bảng với độ rộng cố định.
def format_table_row(name, score):
# Name: căn trái 20 ký tự
# Score: căn phải 10 ký tự
pass
# Test
print(format_table_row("Alice", 95.5))
# "Alice 95.5"- Viết hàm
repeat_charlặp lại một ký tự n lần.
def repeat_char(char, n):
# Code của bạn ở đây
pass
# Test
print(repeat_char("*", 10)) # "**********"- Viết hàm
center_textcăn giữa văn bản với độ rộng cho trước.
def center_text(text, width):
# Code của bạn ở đây
pass
# Test
print(f"'{center_text('Hello', 20)}'")
# "' Hello '"- Viết hàm
add_line_numbersthêm số thứ tự vào mỗi dòng.
def add_line_numbers(text):
# Code của bạn ở đây
pass
# Test
text = "Line one
Line two
Line three"
print(add_line_numbers(text))
# "1. Line one"
# "2. Line two"
# "3. Line three"- Viết hàm
is_palindromekiểm tra xem chuỗi có phải là palindrome không (đọc xuôi ngược giống nhau).
def is_palindrome(text):
# Code của bạn ở đây
# Loại bỏ khoảng trắng và không phân biệt hoa thường
pass
# Test
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
print(is_palindrome("A man a plan a canal Panama")) # True💡 Gợi ý: Dùng slicing
[::-1]để đảo ngược chuỗi