字符串是不可变的:
指的是当修改字符串时,无法直接修改内存数据,而是重新建立新的内存,并修改变量的指针指向新的内存。
与C不同:
与 C 字符串不同的是,Python 字符串不能被改变。向一个索引位置赋值,比如 word[0] = 'm' 会导致错误。
Python 允许处理 unicode 字符串:
1.什么是 unicode 字符串:
Unicode(统一码、万国码、单一码)是计算机科学领域里的一项业界标准,包括字符集、编码方案等。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。
也就是说,Unicode将每种语言的每一个文字都编为了一串数字码,即每一个字符都有一个Unicode与之对应。
2.Python允许使用unicode编码并输出
空行
空行不是python语法的一部分,但是程序代码的一部分,它的作用为:
分隔两段不同功能或含义的代码,便于日后代码的维护或重构。
#hello world
print("hello world")
hello world
#string
print("this ""is ""string""\\\\n")
print("this is a line with a \\\\n")
print(r"this is a line with a \\\\n" "\\\\n" )
print(R"this is a line with a \\\\n" r"\\\\n" )
this is string
this is a line with a
this is a line with a \\\\n
this is a line with a \\\\n\\\\n
#string
str = u"this is an unicode string"
print(str)
str = u"你好"
print(str)
uni = str.encode('unicode-escape').decode()
print(uni)
this is an unicode string
你好
\\\\u4f60\\\\u597d
#string
print('\\\\u4f60\\\\u597d')
你好
#string
str="123456789"
print(str[0:3]) #第一个到第三个字符
123
#string
print(str[0:5:1]) #第1个到第5个字符,步长为1,每两个元素之间隔0个
print(str[0:5:2]) #第1个到第5个字符,步长为2,每两个元素之间隔1个
print(str[0:5:3]) #第1个到第5个字符,步长为3,每两个元素之间隔2个
12345
135
14
str = input("\\\\nplease input a string\\\\n")
print(str)
please input a string
string
string
x=1;y=2 #同行多条语句
print(x) #换行输出
print(y)
print(x,end='-') #不换行输出
print(y,end='')
1
2
1-2
import sys
print('================Python import mode==========================')
print ('命令行参数为:')
for i in sys.argv:
print (i)
================Python import mode==========================
命令行参数为:
E:\\\\Anaconda3\\\\lib\\\\site-packages\\\\ipykernel_launcher.py
-f
C:\\\\Users\\\\24492\\\\AppData\\\\Roaming\\\\jupyter\\\\runtime\\\\kernel-a4851340-47aa-4202-a9b8-eabec0341e24.json