How to realize case conversion in Python
Python how to achieve case conversion, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Convert all fields in a data table to lowercase
# convert a field in the data table to lowercase # load the data source import pandas as pddf_lower = pd.read_excel ("D:\ Jupyter\ data\ Python.xlsx", sheet_name = 1) # Preview df_lower
Convert all to lowercase
# all converted to lowercase df_lower ["goods"] = df_lower ["goods"] .str.lower () df_lower
two。 All strings are converted to lowercase letters
# all strings are converted to lowercase letters str_lower = "BMW" str_lower# str_lower.lower ()
Str_lower.lower ()
3. Convert a field in a data table to uppercase
# convert all fields in the data table to uppercase import pandas as pddf_upper = pd.read_excel ("D:\ Jupyter\ data\ Python.xlsx", sheet_name = 1) df_upper
Df_upper ["goods"] = df_upper ["goods"] .str.upper () df_upper
4. All strings are converted to uppercase letters
# all strings are converted to uppercase letters str_upper = "abcd" str_upper
Str_upper = str_upper.upper () str_upper
5. Convert only the first letter of the word at the beginning of a sentence to uppercase
# convert only the first letter of the word at the beginning of a sentence to capital df_capitalize1 = "i have a lot of money" df_capitalize1
Df_capitalize2 = df_capitalize.capitalize () df_capitalize2
Or
Df_capitalize3 = "who ARE YOU" df_capitalize3
Df_capitalize4 = df_capitalize3.capitalize () df_capitalize4
6. The initials of the title are capitalized
# title initials str_title = "wo yao zi xue wang" str_title
Str_title1 = str_title.title () str_title1
7. Comprehensive application
X = input ("Please enter characters:") y = [] for n in x: if "a"