All Tracks/lecture introduction to python strings/
Đang tải...
Hướng dẫn thử thách
1 / 4

What Are Some Common String Methods?

A method is a function that you call on a value. To call a string method, write the string or its variable name followed by a dot and the method call. You will learn more about methods when you study classes and objects. Here are some common string methods: - `upper()`: Returns a new string with all characters converted to uppercase. ```python my_str = 'hello world' uppercase_my_str = my_str.upper() print(uppercase_my_str) # HELLO WORLD ``` - `lower()`: Returns a new string with all characters converted to lowercase. ```python my_str = 'Hello World' lowercase_my_str = my_str.lower() print(lowercase_my_str) # hello world ``` - `strip()`: Returns a new string with the specified leading and trailing characters removed. If no argument is passed it removes leading and trailing whitespace. ```python my_str = ' hello world ' trimmed_my_str = my_str.strip() print(trimmed_my_str) # "hello world" ``` - `replace(old, new)`: Returns a new string with all occurrences of `old` replaced by `new`. ```python my_str = 'hello world' replaced_my_str = my_str.replace('hello', 'hi') print(replaced_my_str) # hi world ``` - `split(separator)`: Splits a string on a specified separator into a list of strings. A list groups values between square brackets. If no separator is specified, `split()` splits on whitespace. ```python my_str = 'hello world' split_words = my_str.split() print(split_words) # ['hello', 'world'] ``` - `join()`: Joins the strings in a collection into a single string with a separator. ```python my_list = ['hello', 'world'] joined_my_str = ' '.join(my_list) print(joined_my_str) # hello world ``` - `startswith(prefix)`: Returns a boolean indicating if a string starts with the specified prefix. ```python my_str = 'hello world' starts_with_hello = my_str.startswith('hello') print(starts_with_hello) # True ``` - `endswith(suffix)`: Returns a boolean indicating if a string ends with the specified suffix. ```python my_str = 'hello world' ends_with_world = my_str.endswith('world') print(ends_with_world) # True ``` - `find(substring)`: Returns the index of the first occurrence of `substring`, or `-1` if it doesn't find one. ```python my_str = 'hello world' world_index = my_str.find('world') print(world_index) # 6 ``` - `count(substring)`: Returns the number of times a substring appears in a string. ```python my_str = 'hello world' o_count = my_str.count('o') print(o_count) # 2 ``` - `capitalize()`: Returns a new string with the first character capitalized and the other characters lowercased. ```python my_str = 'hello world' capitalized_my_str = my_str.capitalize() print(capitalized_my_str) # Hello world ``` - `isupper()`: Returns `True` if all letters in the string are uppercase and `False` if not. ```python my_str = 'hello world' is_all_upper = my_str.isupper() print(is_all_upper) # False ``` - `islower()`: Returns `True` if all letters in the string are lowercase and `False` if not. ```python my_str = 'hello world' is_all_lower = my_str.islower() print(is_all_lower) # True ``` - `title()`: Returns a new string with the first letter of each word capitalized. ```python my_str = 'hello world' title_case_my_str = my_str.title() print(title_case_my_str) # Hello World ```
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.py
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵
Test Output
Thử thách này không có bài test tự động. Hãy quan sát kết quả trực tiếp ở khung Preview.