首頁 >後端開發 >Python教學 >如何在 Python 中將字串轉換為小寫?

如何在 Python 中將字串轉換為小寫?

Patricia Arquette
Patricia Arquette原創
2024-11-15 13:10:02231瀏覽

How do I Convert a String to Lowercase in Python?

Lowercasing a String in Python: A Simple Guide

Converting a string to lowercase is a common task in programming. Python provides an effortless method to accomplish this using the lower() function.

How to Lowercase a String

To convert a string to lowercase, simply utilize the lower() function as follows:

string.lower()

Example

Consider the string "Kilometers". To convert it to lowercase, we can use:

lowercase_string = "Kilometers".lower()

This will result in the following lowercase string:

lowercase_string = "kilometers"

Additional Notes

  • The lower() function operates on a copy of the original string and does not modify the original.
  • The converted string is returned as a new copy, allowing you to assign it to a different variable.
  • lower() converts all uppercase characters to their lowercase equivalents.

Conclusion

Lowercasing a string in Python is a straightforward process using the lower() function. This function provides a simple and efficient way to convert strings to lowercase for various applications.

以上是如何在 Python 中將字串轉換為小寫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn