Share u python string

doansocks

New member
### Phương thức chuỗi Python

Các chuỗi Python là bất biến, điều đó có nghĩa là chúng không thể thay đổi sau khi chúng được tạo ra.Tuy nhiên, có một số phương pháp có thể được sử dụng để thao tác các chuỗi.

#### 1. `str.capitalize ()`

Phương thức `str.capitalize ()` Trả về một bản sao của chuỗi với chữ cái đầu tiên được viết hoa.Ví dụ:

`` `Python
>>> str.capitalize ('Hello World')
'Chào thế giới'
`` `

#### 2. `str.lower ()`

Phương thức `str.Lower ()` trả về một bản sao của chuỗi với tất cả các chữ cái bị hạ thấp.Ví dụ:

`` `Python
>>> str.lower ('Hello World')
'Chào thế giới'
`` `

#### 3. `str.upper ()`

Phương thức `str.upper ()` trả về một bản sao của chuỗi với tất cả các chữ cái được sử dụng.Ví dụ:

`` `Python
>>> str.upper ('Hello World')
'CHÀO THẾ GIỚI'
`` `

#### 4. `str.strip ()`

Phương thức `str.strip ()` loại bỏ khoảng trắng từ đầu và cuối của chuỗi.Ví dụ:

`` `Python
>>> str.strip ('Hello World')
'Chào thế giới'
`` `

#### 5. `str.replace ()`

Phương thức `str.replace ()` thay thế tất cả các lần xuất hiện của một chuỗi con được chỉ định bằng một chuỗi con khác.Ví dụ:

`` `Python
>>> str.replace ('Xin chào', 'Tạm biệt', 'Hello World')
'Tạm biệt thế giới'
`` `

#### 6. `str.split ()`

Phương thức `str.split ()` chia một chuỗi thành một danh sách các chuỗi con dựa trên một dấu phân cách được chỉ định.Ví dụ:

`` `Python
>>> str.split (',', 'Xin chào, thế giới')
['Chào thế giới']
`` `

#### 7. `str.join ()`

Phương thức `str.join ()` tham gia một danh sách các chuỗi vào một chuỗi duy nhất, sử dụng một dấu phân cách được chỉ định.Ví dụ:

`` `Python
>>> '' .join (['xin chào', 'thế giới']))
'Chào thế giới'
`` `

#### 8. `str.find ()`

Phương thức `str.find ()` trả về chỉ mục của lần xuất hiện đầu tiên của một chuỗi con được chỉ định trong một chuỗi.Nếu không tìm thấy chuỗi con, phương thức sẽ trả về -1.Ví dụ:

`` `Python
>>> str.find ('xin chào', 'xin chào thế giới')
0
`` `

#### 9. `str.rfind ()`

Phương thức `str.rfind ()` trả về chỉ mục của lần xuất hiện cuối cùng của một chuỗi con được chỉ định trong một chuỗi.Nếu không tìm thấy chuỗi con, phương thức sẽ trả về -1.Ví dụ:

`` `Python
>>> str.rfind ('xin chào', 'xin chào thế giới')
6
`` `

#### 10. `str.isalpha ()`

Phương thức `str.isalpha ()` trả về true nếu chuỗi chỉ chứa các ký tự chữ cái và sai.Ví dụ:

`` `Python
>>> str.isalpha ('Hello World')
ĐÚNG VẬY
`` `

#### 11. `str.isdigit ()`

Phương thức `str.isdigit ()` trả về true nếu chuỗi chỉ chứa các chữ số và sai.Ví dụ:

`` `Python
>>> str.isdigit ('123456')
ĐÚNG VẬY
`` `

#### 12. `str.isalnum ()`

Phương thức `str.isalnum ()` trả về true nếu chuỗi chỉ chứa các ký tự chữ và số (chữ cái và chữ số) và sai.Ví dụ:

`` `Python
>>> str.isalnum ('hello1234')
ĐÚNG VẬY
`` `

#### 13. `str.startswith ()`

Phương thức `str.startswith ()` trả về true nếu chuỗi bắt đầu bằng một chuỗi con được chỉ định và sai.Ví dụ:

`` `Python
>>> str.startswith ('xin chào', 'xin chào thế giới')
ĐÚNG VẬY
`` `

#### 14. `str.endswith ()`

`Str.endswith ()`
=======================================
### Python String Methods

Python strings are immutable, which means they cannot be changed once they are created. However, there are a number of methods that can be used to manipulate strings.

#### 1. `str.capitalize()`

The `str.capitalize()` method returns a copy of the string with the first letter capitalized. For example:

```python
>>> str.capitalize('hello world')
'Hello world'
```

#### 2. `str.lower()`

The `str.lower()` method returns a copy of the string with all letters lowercased. For example:

```python
>>> str.lower('Hello World')
'hello world'
```

#### 3. `str.upper()`

The `str.upper()` method returns a copy of the string with all letters uppercased. For example:

```python
>>> str.upper('Hello World')
'HELLO WORLD'
```

#### 4. `str.strip()`

The `str.strip()` method removes whitespace from the beginning and end of a string. For example:

```python
>>> str.strip(' Hello World ')
'Hello World'
```

#### 5. `str.replace()`

The `str.replace()` method replaces all occurrences of a specified substring with another substring. For example:

```python
>>> str.replace('Hello', 'Goodbye', 'Hello World')
'Goodbye World'
```

#### 6. `str.split()`

The `str.split()` method splits a string into a list of substrings based on a specified delimiter. For example:

```python
>>> str.split(',', 'Hello, World')
['Hello', 'World']
```

#### 7. `str.join()`

The `str.join()` method joins a list of strings into a single string, using a specified delimiter. For example:

```python
>>> ' '.join(['Hello', 'World'])
'Hello World'
```

#### 8. `str.find()`

The `str.find()` method returns the index of the first occurrence of a specified substring in a string. If the substring is not found, the method returns -1. For example:

```python
>>> str.find('Hello', 'Hello World')
0
```

#### 9. `str.rfind()`

The `str.rfind()` method returns the index of the last occurrence of a specified substring in a string. If the substring is not found, the method returns -1. For example:

```python
>>> str.rfind('Hello', 'Hello World')
6
```

#### 10. `str.isalpha()`

The `str.isalpha()` method returns True if the string contains only alphabetic characters, and False otherwise. For example:

```python
>>> str.isalpha('Hello World')
True
```

#### 11. `str.isdigit()`

The `str.isdigit()` method returns True if the string contains only digits, and False otherwise. For example:

```python
>>> str.isdigit('123456')
True
```

#### 12. `str.isalnum()`

The `str.isalnum()` method returns True if the string contains only alphanumeric characters (letters and digits), and False otherwise. For example:

```python
>>> str.isalnum('Hello1234')
True
```

#### 13. `str.startswith()`

The `str.startswith()` method returns True if the string starts with a specified substring, and False otherwise. For example:

```python
>>> str.startswith('Hello', 'Hello World')
True
```

#### 14. `str.endswith()`

The `str.endswith()`
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top