Hướng dẫn dùng captilized python
Chuỗi (String) trong Python Show Hàm center() trong Python Hàm capitalize() trong Python trả về một bản sao của chuỗi với chữ cái đầu tiên được viết hoa. Nội dung chính
Cú phápCú pháp của hàm capitalize() trong Python: str.capitalize() Ví dụ hàm capitalize() trong PythonVí dụ sau minh họa cách sử dụng của hàm capitalize() trong Python. str1 = "hello world!" str2 = "hello Python!" print (str1.capitalize()) print (str2.capitalize()) Chạy chương trình Python trên sẽ cho kết quả: Hello world! Hello python! Chuỗi (String) trong Python Hàm center() trong Python Chuỗi (String) trong Python Hàm center() trong Python Hàm capitalize() trong Python trả về một bản sao của chuỗi với chữ cái đầu tiên được viết hoa. Nội dung chính
Cú phápCú pháp của hàm capitalize() trong Python: str.capitalize() Ví dụ hàm capitalize() trong PythonVí dụ sau minh họa cách sử dụng của hàm capitalize() trong Python. str1 = "hello world!" str2 = "hello Python!" print (str1.capitalize()) print (str2.capitalize()) Chạy chương trình Python trên sẽ cho kết quả: Hello world! Hello python! Chuỗi (String) trong Python Hàm center() trong Python Python has many built-in methods that perform operations on strings. One of the operations is to change the case of letters. We'll see different ways to change the case of the letters in a string, and go through many examples in each section, and finally go through some of the applications. Nội dung chính
Python strings are immutable, which means that once created these strings cannot be modified. As a consequence, most of the functions that operate on strings actually return a modified copy of the string; the original string is unchanged. Let's take a look at some of the ways we can manipulate strings to change the case of words. Different casesWe have covered different cases like:
Capitalize the first letter using capitalize()To capitalize the first letter, use the Syntax: Example:
Notice that the function Convert the entire string to upper-caseTo convert all the letters of the string to uppercase, we can use the Syntax: Example:
Convert the entire string to lower-caseTo convert all the letters of the string to lowercase, we can use the Syntax: Example:
Capitalize first letter of each wordTo capitalize the first letter of each word use the Syntax: Example:
Notice that while each word was capitalized, the rest of the letters were converted to lowercase. ApplicationsNow that we know how to perform basic case-manipulation, lets see how can this be useful for us? PromptsA prompt is a text message that prompts the user to enter some input. For example, a prompt to enter a number. A simple prompt could be to allow user to provide a permission by typing the word 'Yes'. In python "Yes", "yeS" or "YES" are all distinct strings, and we need to be able to accept all variants of those. We can write code to check each of those individually, but the Let's go through some sample code
Here, we checked for input 'Yes' and every other input would be considered as a 'No'. This is sometimes useful, but adding a check for 'No' might be required; especially when you want to provide additional messages when user types invalid inputs like a number, instead of 'Yes' or 'No'. Searching namesUsually, to speed up searching process, names are better stored in the same case. This is done only when the changing the case would not cause any problems. The reason why you'd want to do this could be to make searching faster. Searching for someone named Alice as "ALice", "ALICE" or "alice" could be confusing and lead to unnecessary work. To overcome this problem, names could be stored in upper-case by using the E-mail addressesE-mail addresses are not case-sensitive. So in an application that requires sign in, you have to be able to handle all types of email address inputs. Email addresses like "", "" or "" are all same email addresses, but different python strings. Thus the input needs to be converted to lower-case, which is common format for email addresses. Here, the
here, we have assumed that there is a function |