- Không xác định.py
- Ngày 25 tháng 12 năm 2021
Trong Python, ký tự '\ n' đại diện cho một dòng mới.'\n' character represents a new line.
Hướng dẫn này sẽ tiết lộ các cách khác nhau để kiểm tra xem một chuỗi có chứa một dòng mới không.
Bắt đầu nào.
Kiểm tra xem một chuỗi có chứa newlines bằng toán tử 'in' không
Với toán tử 'IN', chúng ta có thể kiểm tra giá trị được chỉ định trong một chuỗi. Tuy nhiên, phương pháp này trả về đúng nếu giá trị tồn tại. Nếu không, trả về sai.'in' operator, we can check for a specified value in a string. However, this method returns True if the value exists. Otherwise, returns False.
Cú pháp
'\n' in my_string
Thí dụ
Chúng tôi sẽ kiểm tra một dòng mới trong chuỗi bằng & nbsp; toán tử '\ n' trong ví dụ sau.
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
Output:
True
Đầu ra là đúng có nghĩa là my_string chứa một dòng mới.True that's means my_string contains a new line.
Bây giờ, hãy xóa tất cả các dòng mới khỏi chuỗi và xem đầu ra.
# String
my_string = '''Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation.'''
# Check if Newline is exists on my_string
print['\n' in my_string]
Output:
False
Ví dụ "làm gì đó"
Nếu bạn muốn làm điều gì đó sau khi kiểm tra, hãy xem ví dụ sau:
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
if '\n' in my_string:
# Do something
print[True]
else:
# Do something
print[False]
Output:
True
Kiểm tra xem một chuỗi có chứa newlines bằng regex không
Chúng ta cũng có thể sử dụng Regex để kiểm tra xem một chuỗi có chứa dòng mới không.
Cú pháp
# search function
re.search['\n', my_string]
# findall function
re.findall['\n', my_string]
Thí dụ
Chúng tôi sẽ kiểm tra một dòng mới trong chuỗi bằng & nbsp; toán tử '\ n' trong ví dụ sau. returns the object if there is a match. Otherwise, it returns None.
Đầu ra là đúng có nghĩa là my_string chứa một dòng mới.
import re
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Search Newlines on my_string
regex = re.search['\n', my_string]
# Result
print[regex]
Output:
Bây giờ, hãy xóa tất cả các dòng mới khỏi chuỗi và xem đầu ra.
Ví dụ "làm gì đó" Returns a list of all matches; otherwise, it returns an empty list.
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
0Output:
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
1Nếu bạn muốn làm điều gì đó sau khi kiểm tra, hãy xem ví dụ sau:
Chào mừng! Nhân vật dòng mới trong Python được sử dụng để đánh dấu phần cuối của một dòng và bắt đầu một dòng mới. Biết cách sử dụng nó là điều cần thiết nếu bạn muốn in đầu ra vào bảng điều khiển và làm việc với các tệp.The new line character in Python is used to mark the end of a line and the beginning of a new line. Knowing how to use it
is essential if you want to print output to the console and work with files. Trong bài viết này, bạn sẽ học: Hãy bắt đầu nào! ✨ Nhân vật dòng mới trong Python là: Nó được làm bằng hai ký tự: Nếu bạn thấy ký tự này trong một chuỗi, điều đó có nghĩa là dòng hiện tại kết thúc tại điểm đó và một dòng mới bắt đầu ngay sau khi nó: Bạn cũng có thể sử dụng ký tự này trong F-String:f-strings: Theo mặc định, các câu lệnh in thêm một ký tự dòng mới "phía sau hậu trường" ở cuối chuỗi. Như thế này:🔹 Nhân vật dòng mới
4.# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
2# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
Nhân vật dòng mới trong các câu lệnh in
Điều này xảy ra bởi vì, theo tài liệu Python:
Giá trị mặc định của tham số
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
5 của hàm # String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
6 tích hợp là # String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
7, do đó, một ký tự dòng mới được thêm vào chuỗi.Mẹo: ỨNG DỤNG có nghĩa là "Thêm vào cuối". Append means "add to the end".
Đây là định nghĩa chức năng:
Lưu ý rằng giá trị của
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
5 là # String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
7, vì vậy điều này sẽ được thêm vào cuối chuỗi.Nếu bạn chỉ sử dụng một câu lệnh in, bạn sẽ không nhận thấy điều này vì chỉ có một dòng sẽ được in:
Nhưng nếu bạn sử dụng một số câu lệnh in hết trong tập lệnh Python:
Đầu ra sẽ được in bằng các dòng riêng biệt vì
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
7 đã được thêm "phía sau hậu trường" vào cuối mỗi dòng:Chúng ta có thể thay đổi hành vi mặc định này bằng cách tùy chỉnh giá trị của tham số
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
5 của hàm # String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
6.Nếu chúng ta sử dụng giá trị mặc định trong ví dụ này:
Chúng tôi thấy đầu ra được in thành hai dòng:
Nhưng nếu chúng ta tùy chỉnh giá trị của
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
5 và đặt nó thành True
4Một không gian sẽ được thêm vào cuối chuỗi thay vì ký tự dòng mới
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
7, do đó, đầu ra của hai câu lệnh in sẽ được hiển thị trong cùng một dòng:Bạn có thể sử dụng điều này để in một chuỗi các giá trị trong một dòng, như trong ví dụ này:
Đầu ra là:
Mẹo: Chúng tôi thêm một câu lệnh có điều kiện để đảm bảo rằng dấu phẩy sẽ không được thêm vào số cuối cùng của chuỗi. We add a conditional statement to make sure that the comma will not be added to the last number of the sequence.
Tương tự, chúng ta có thể sử dụng điều này để in các giá trị của một điều không thể sử dụng được trong cùng một dòng:
Đầu ra là:
Mẹo: Chúng tôi thêm một câu lệnh có điều kiện để đảm bảo rằng dấu phẩy sẽ không được thêm vào số cuối cùng của chuỗi.
Tương tự, chúng ta có thể sử dụng điều này để in các giá trị của một điều không thể sử dụng được trong cùng một dòng:
🔸 ký tự dòng mới trong các tệp
# String
my_string = '''
Python is an interpreted high-level general-purpose programming language.
Its design philosophy emphasizes code readability with its use of significant indentation.
'''
# Check if Newline exists in my_string
print['\n' in my_string]
3Đầu ra là:
Mẹo: Chúng tôi thêm một câu lệnh có điều kiện để đảm bảo rằng dấu phẩy sẽ không được thêm vào số cuối cùng của chuỗi.
Tương tự, chúng ta có thể sử dụng điều này để in các giá trị của một điều không thể sử dụng được trong cùng một dòng:Notice that only the last line of the file doesn't end with a new line character.
Tóm tắt
- Nhân vật dòng mới trong Python là
7. Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.# String my_string = ''' Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. ''' # Check if Newline exists in my_string print['\n' in my_string]
- Bạn có thể in các chuỗi mà không cần thêm một dòng mới với
1, mà# String my_string = '''Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation.''' # Check if Newline is exists on my_string print['\n' in my_string]
2 là ký tự sẽ được sử dụng để tách các dòng.# String my_string = '''Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation.''' # Check if Newline is exists on my_string print['\n' in my_string]
Tôi thực sự hy vọng rằng bạn thích bài viết của tôi và thấy nó hữu ích. Bây giờ bạn có thể làm việc với nhân vật dòng mới trong Python.Now you can work with the new line character in Python.
Kiểm tra các khóa học trực tuyến của tôi. Theo dõi tôi trên Twitter. ⭐
Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu