zoukankan      html  css  js  c++  java
  • Python(字符串操作实例1)一个字符串用空格隔开

    # 将字符中单词用空格隔开
    # 已知传入的字符串中只有字母,每个单词的首字母大写,
    # 请将每个单词用空格隔开,只保留第一个单词的首字母大写传入:“HelloMyWorld”
    # 返回“Hello my world”


    # 给定一个字符串
    inStr = "HelloMyWorld"
    # 把字符串转换成列表
    str_list = list(inStr)

    # 用循环取出每一个元素
    for i in inStr:
    # 判断元素是否是大写
    if i.isupper():
    # 如果是大写就记录下标位置
    index = str_list.index(i)
    # 判断如果是第一个首字母则跳出本次循环
    if index == 0:
    continue
    # 修改数据,把大写转换成小宝和添加空格
    str_list[index] = i.lower()
    str_list.insert(index," ")

    # 转换成字符串
    outStr = "".join(str_list)
    print(outStr)
  • 相关阅读:
    PHP验证码
    c#属性
    框架数据连接类
    mysqli
    C# 委托
    Windows系统,文件和文件夹命名规则:
    固态硬盘与普通硬盘的区别
    windows快捷键
    1.认识计算机
    计算机题解
  • 原文地址:https://www.cnblogs.com/yunlongaimeng/p/8748003.html
Copyright © 2011-2022 走看看