Python基礎--變量定義、字符串切片及編碼

1.定義變量及打印輸出

a=1
#python輸出方式
print(a)
#%s %d 限制輸出的是字符串還是整數數字
name='M-x-M'
print('My name is %s'  % name)
age=18
print('I`m %d years old '% age)
#普通打印
job='python'
print('I`m learning',job)
#多行打印
print('''--------day1------------
多行打印效果測試
My name is %s' ,I`m learning %s '''%(name,job))

2.字符串切片

str='開始學python'
print(str[0:1])
print(str[3:])
print(str[:-6])

Python基礎--變量定義、字符串切片及編碼
3.字符串轉譯 加\
4.字符串編碼轉換
4.1轉換爲Unicode

str='字符串'
b_str=str.encode()

4.2轉爲字符串

str1=b_str.decode()

Python基礎--變量定義、字符串切片及編碼
5.split()方法用於切割字符串

str1='hello world.txt'
cmd,filename=str1.split()
print(filename)
>>:
world.text
#--------------------------------
str1='hello|world.txt'
cmd,filename=str1.split('|')
print(filename)
>>:
world.text

6.strip()方法用於移除字符串前後的指定字符,默認爲空格

str1='   hello world     '
print(str1.strip())
>>:
hello world
#---------------------------
str1=str1='|hello|world|'
print(str1.strip('|'))
>>:
hello|world
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章