Hướng dẫn what is end in python - cái gì đang kết thúc trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Theo mặc định, hàm print () của Python kết thúc bằng một dòng mới. Một lập trình viên có nền C/C ++ có thể tự hỏi làm thế nào để in mà không có dòng mới. Chức năng Python Python Print () đi kèm với một tham số gọi là ‘kết thúc. Theo mặc định, giá trị của tham số này là ‘\ n, tức là ký tự dòng mới. & Nbsp;

    Python3

    Ví dụ 1:

    Ở đây, chúng ta có thể kết thúc một câu lệnh in với bất kỳ ký tự/chuỗi nào bằng tham số này. & NBSP;

    Output:

    Welcome to GeeksforGeeks

    Python@GeeksforGeeks5Python@GeeksforGeeks6Python@GeeksforGeeks7Python@GeeksforGeeks8Python@GeeksforGeeks9 GFG 09-12-2016 Red,Green,Blue@geeksforgeeks0GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python@GeeksforGeeks5Python@GeeksforGeeks6GFG 09-12-2016 Red,Green,Blue@geeksforgeeks4GFG 09-12-2016 Red,Green,Blue@geeksforgeeks5Python@GeeksforGeeks9 GFG 09-12-2016 Red,Green,Blue@geeksforgeeks0GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python3

    Python@GeeksforGeeks5Python@GeeksforGeeks6print("Studytonight",) print("is awesome") print("Studytonight", end= "\n") print("is awesome")1GFG 09-12-2016 Red,Green,Blue@geeksforgeeks5Python@GeeksforGeeks9print("Studytonight",) print("is awesome") print("Studytonight", end= "\n") print("is awesome")4GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python@GeeksforGeeks5Python@GeeksforGeeks6GFG 09-12-2016 Red,Green,Blue@geeksforgeeks4GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Output:

    Python@GeeksforGeeks

    Ví dụ 2: & nbsp;

    Thêm một chương trình để chứng minh hoạt động của tham số cuối. & NBSP;

    Python3

    Ví dụ 3:

    Python@GeeksforGeeks5Python@GeeksforGeeks6Studytonight is awesome Studytonight is awesome2GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python@GeeksforGeeks5Python@GeeksforGeeks6print("Studytonight", end=' ') print("is awesome")6Studytonight is awesome Studytonight is awesome3print("Studytonight", end=' ') print("is awesome")8Studytonight is awesome Studytonight is awesome3Studytonight is awesome0Studytonight is awesome Studytonight is awesome5Python@GeeksforGeeks9Studytonight is awesome3GFG 09-12-2016 Red,Green,Blue@geeksforgeeks5Python@GeeksforGeeks9Studytonight is awesome6GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python@GeeksforGeeks5Python@GeeksforGeeks6print("Hi", end=' Studytonight ') print("you are awesome")0Studytonight is awesome Studytonight is awesome3print("Hi", end=' Studytonight ') print("you are awesome")2Studytonight is awesome Studytonight is awesome3print("Hi", end=' Studytonight ') print("you are awesome")4Studytonight is awesome Studytonight is awesome5Python@GeeksforGeeks9print("Hi", end=' Studytonight ') print("you are awesome")7GFG 09-12-2016 Red,Green,Blue@geeksforgeeks5Python@GeeksforGeeks9print("Studytonight",) print("is awesome") print("Studytonight", end= "\n") print("is awesome")4GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Python@GeeksforGeeks5Python@GeeksforGeeks6Hi Studytonight you are awesome4GFG 09-12-2016 Red,Green,Blue@geeksforgeeks1

    Hàm in () sử dụng tham số SEP để phân tách các đối số và kết thúc sau đối số cuối cùng.

    GFG 09-12-2016 Red,Green,Blue@geeksforgeeks

    Tất cả chúng ta đã viết nhiều câu lệnh in bằng Python có hoặc không có tham số. Nhưng bạn đã bao giờ tự hỏi tầm quan trọng của việc sử dụng các tham số SEP và kết thúc cho câu lệnh Python@GeeksforGeeks5 chưa?sep and end parameters to the Python@GeeksforGeeks5 statement?

    Trong bài đăng này, chúng tôi sẽ thảo luận về cách các tham số ‘SEP và‘ end có thể được sử dụng để thay đổi cách mà nội dung của phương thức Python@GeeksforGeeks5 được in trên bảng điều khiển.sep’ and ‘end’ parameters can be used to change the way in which the contents of the Python@GeeksforGeeks5 method are printed on the console.

    1. Tham số Hi Studytonight you are awesome8

    Tham số cuối được sử dụng để nối bất kỳ chuỗi nào ở cuối đầu ra của câu lệnh Python@GeeksforGeeks5 trong Python.end parameter is used to append any string at the end of the output of the Python@GeeksforGeeks5 statement in python.

    Theo mặc định, phương thức Python@GeeksforGeeks5 kết thúc bằng một dòng mới. Điều này có nghĩa là không cần chỉ định rõ ràng đầu vào tham số là '\ n'. Các lập trình viên có nền trong C/C ++ có thể thấy điều này thú vị.newline. This means there is no need to explicitly specify the parameter end as '\n'. Programmers with a background in C/C++ may find this interesting.

    Chúng ta hãy xem xét cách thay đổi giá trị của tham số cuối thay đổi nội dung của câu lệnh in trên màn hình.end parameter changes the contents of the print statement onscreen.

    Ví dụ dưới đây chứng minh rằng việc truyền '\ n' hoặc không chỉ định tham số cuối đều mang lại cùng một kết quả. Thực hiện 2 dòng mã tại một thời điểm để xem kết quả.'\n' or not specifying the end parameter both yield the same result. Execute 2 lines of code at a time to see the result.

    print("Studytonight",) print("is awesome") print("Studytonight", end= "\n") print("is awesome")

    Output:

    Studytonight is awesome Studytonight is awesome

    Mặt khác, chuyển khoảng trắng đến tham số cuối cho biết rằng ký tự cuối phải được xác định bằng khoảng trắng chứ không phải là dòng mới (là mặc định).end parameter indicates that the end character has to be identified by whitespace and not a newline (which is the default).

    print("Studytonight", end=' ') print("is awesome")

    Output:

    Studytonight is awesome

    Ví dụ dưới đây cho thấy rằng bất kỳ giá trị nào cũng có thể được chuyển đến tham số cuối và dựa trên nội dung trong câu lệnh in, giá trị cuối được hiển thị.end parameter and based on the content in the print statement, the end value gets displayed.

    print("Hi", end=' Studytonight ') print("you are awesome")

    Output:

    Hi Studytonight you are awesome

    2. Tham số print("Study", "tonight") print("Study", "tonight", sep = ' ')1

    Đôi khi, chúng ta có thể muốn in nhiều giá trị trong một chương trình Python một cách có thể đọc được. Đây là khi cuộc tranh luận SEP đến chơi. Các đối số được truyền cho chương trình có thể được phân tách bằng các giá trị khác nhau. Giá trị mặc định cho SEP là khoảng trắng. Tham số SEP chủ yếu được sử dụng để định dạng các chuỗi cần được in trên bảng điều khiển và thêm một khoảng cách giữa các chuỗi để được in. Tính năng này mới được giới thiệu trong phiên bản Python 3.x.sep comes to play. The arguments passed to the program can be separated by different values. The default value for sep is whitespace. The sep parameter is primarily used to format the strings that need to be printed on the console and add a separator between strings to be printed. This feature was newly introduced in Python 3.x version.

    Ví dụ dưới đây cho thấy rằng việc chuyển tham số SEP dưới dạng khoảng trắng hoặc không vượt qua SEP hoàn toàn không tạo ra sự khác biệt. Thực hiện mọi dòng mã để xem kết quả.sep parameter as whitespace or not passing the sep at all doesn't make a difference. Execute every line of code to see the result.

    print("Study", "tonight") print("Study", "tonight", sep = ' ')

    Output:

    Python@GeeksforGeeks0

    Ví dụ dưới đây cho thấy các giá trị khác nhau được truyền đến tham số SEP.sep parameter.

    Python@GeeksforGeeks1

    Output:

    Python@GeeksforGeeks2

    Lưu ý: Tham số SEP, được sử dụng cùng với tham số cuối thường được sử dụng trong mã sản xuất để in dữ liệu theo kiểu có thể đọc được. The sep parameter, used in conjunction with the end parameter is generally used in production code to print data in a readable fashion.

    Python@GeeksforGeeks3

    Output:

    Python@GeeksforGeeks4

    Conclusion:

    Trong bài đăng này, chúng tôi hiểu tầm quan trọng của các tham số 'SEP' và 'END' được sử dụng trong phương pháp Python@GeeksforGeeks5.sep' and 'end' parameters that are used in the Python@GeeksforGeeks5 method.

    Bạn cũng có thể thích

    • Kiểm tra xem hai danh sách có giống hệt nhau bằng cách sử dụng Python không

    • Chức năng 'SetDefault' trong Python

    • Chức năng 'cắt' trong Python

    • Python *args và ** biến Kwargs

    Chủ đề