编写一个程序,在单词中的每个大写字母前添加空格,然后将字符串中的每个字符转换为小写。
- 定义函数
add_space_before_capital()
,它接受一个参数word
。 - 在函数中,在字符串的每个大写字母前添加空格,并转为小写字符串后返回。
示例输入
myNewComputer
示例输出
my new computer
本题涉及字符大小写判断,字符串拼接函数join,字符在字符串中的索引获取函数index,大小写转换。
ch.isupper()判断该字符是否为大写。
ch.islower()判断该字符是否为大写。
join拼接函数格式:string.join(iterable[str]),其中iterable[str]是要拼接的元素,join函数只接受一个参数,而string则是用于拼接的字符,也可为空。该函数返回拼接后的新元素。
如return '*'.join(['1','2','3']) 返回1*2*3。
string.index(ch)表示获取ch在string中的索引。
string.lower()表示将string中的大写字符转换为小写字符。
def add_space_before_capital(word):
# 在此处编写你的代码
for ch in word:
if ch.isupper():
word=' '.join([word[:word.index(ch)],word[word.index(ch):]])
return word.lower()
# 获取用户输入
word = input()
# 调用函数
print(add_space_before_capital(word))