String conversion

Character divided, int integer str string bool Boolean

Three types can be converted to each other, but can only convert convertible

String

  • After use character formatting, it will generate a new value, the old value of the variable constant.

  • Variable .uppre () converts uppercase

    • The variable string unified capitalized.
  • Variable .lower () converts lowercase

    • The variable string unified lowercase.
  • Mainly for the user to enter a default case are correct. For example codes, not case-sensitive.

    value = "adck WB"
    new_value = value.upper()  # 这里使用转换后,会生成一个新的变量,老变量不变。
    new_value1 = value.lower() #所有字符转换成小写
    print(new_value,new_value1)
  • Variable .isbigit () determines whether the digital

    • Determining whether the string input by the user can be converted into digital. No to False for Ttrue
  • Variable .strip () to space

    • Remove the space character on both sides. User input mainly used to eliminate extra spaces on both sides, prevent errors.
    • Variable .lstrip () to remove the character to the left spaces.
    • Variable .rstrip () to remove the character to the left spaces.
    # ####### .strip/.rstrip/.lstrip 去两边空格/去左空格/去右空格  .isdigit 判断是否为数字 #######
    content = input('请输入数字编号:')
    new_content = content.strip()  #去除输入的字符串两边的空格。防止用户出错
    print(content,'<-这是原输入->这是去空格后',new_content)
    if new_content.isdigit():     #判断输入的字符串是否为数字。
        print(new_content,'输入正确')
    else:
        print(new_content,'请输入纯数字,且中间不能有空格')
  • Variable .replace () replacement

    • Keyword substitution
    # #######.replace 替换 #######
    message = input('请输入发布你的信息:')
    new_message = message.replace('台独','**',2) # 要替换的字符,替换为,替换几个
    print(new_message)
  • Segmentation variable .split

    • The character segmentation
    # #######.split/lsplit/.rsplit 切割/左分割/右分割 #######
    message = "我今天学习,学习如何。使用切割,但是还不。太懂。我今天学习,学习如何使用切割,但是还不太懂。"
    mage = message.split('。',2) # 将字符串分割。(以什么为分割点,分割成几部分)
    print(mage)

Guess you like

Origin www.cnblogs.com/Dtime/p/10962533.html