学习笔记(5):零基础掌握 Python 入门到实战-深入浅出字符串(一)

    科技2025-09-20  103

    立即学习:https://edu.csdn.net/course/play/26676/338774?utm_source=blogtoedu

    >>> import sys >>> sys.getdefaultencoding() 'utf-8' >>> a = 'pythn' >>> a 'pythn' >>> 250 250 >>> type(250) <class 'int'> >>> a = '250' >>> type(a) <class 'str'> >>> str(250) '250'

    #str表示字符串

    可以通过int(),str()完成字符串及整数之间的转换

    >>> "What's your name?" "What's your name?" >>> 'what\'s your name?' "what's your name?"

    #\为转义符,用来区分一个句子中不同单引号的含义

    >>> m = "python" >>> n = "book" >>> m + n 'pythonbook' 相加的类型必须相同

    对于序列来说只有+,*;没有-,/

    >>> len(m) 6 >>> name = "李卓雅" >>> len(name) 3

    s = "齐天大圣" b = s.encode('utf-8') print(b) c = len(s) print(c) d = len(b) print(d)

    len()统计的为字符数目,不为字节数目,中文在utf-8中占3or4个字节(在不同编码规则下每个字符对应的字节数有所差异)

    >>> m = "python" >>> m 'python' >>> 'p' in m True >>> 'c' in m False

    以上操作对字符串及序列都成立

    相关资源:jdk-8u281-windows-x64.exe
    Processed: 0.008, SQL: 8