在Python中,可以使用以下方法进行大小写转换:

  1. lower():将字符串中的所有字符转换为小写。
string = 'Hello World'
lower_string = string.lower()
print(lower_string)  # 输出:hello world
  1. upper():将字符串中的所有字符转换为大写。
string = 'Hello World'
upper_string = string.upper()
print(upper_string)  # 输出:HELLO WORLD
  1. capitalize():将字符串的首字母转换为大写,其余字符转换为小写。
string = 'hello world'
capitalized_string = string.capitalize()
print(capitalized_string)  # 输出:Hello world
  1. title():将字符串中每个单词的首字母转换为大写。
string = 'hello world'
title_string = string.title()
print(title_string)  # 输出:Hello World
  1. swapcase():将字符串中的大写字母转换为小写,小写字母转换为大写。
string = 'Hello World'
swapcase_string = string.swapcase()
print(swapcase_string)  # 输出:hELLO wORLD
Python 字符串大小写转换方法详解

原文地址: https://www.cveoy.top/t/topic/fRnd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录