1. 程式人生 > >Python編程從入門到實踐-第2章-字符串

Python編程從入門到實踐-第2章-字符串

string als 作用 小寫 mes 換行符 col scrip The

一、字符串:一系列字符,python中用一號括起來的即為字符串,可單可雙

"This is a string."
This is also a string.

#引號靈活性的作用:可在字符串中包含引號和撇號

I told my friend,"python is my favorite language!"
"the language ‘Python‘ is named after Monty Python,not the snake."

#1.使用方法修改字符串大的小寫

name = "ada lovelace"
print(name.title())

name 
= "ada lovelace" print(name.upper()) name = "ada lovelace" print(name.lower())

#2.合並(拼接)字符串

first_name = ada
last_name = lovelace
full_name = first_name +   +last_name

print(full_name)

print ("Hello," + full_name.title() + "!")

message = "Hello," + full_name.title() + "!"
print (message)


#3.制表符或換行符添加空白
#制表符:\t

print (python)
print (\tpython)

#換行符:\n

print (python)
print (\npython)
print (Languages:\nPython\nC\nJavaScript)

#同時使用制表符和換行符
print (‘Languages:\n\tPython\n\tC\n\tJavaScript‘)


#4.刪除空白:rstrip,lstrip,strip
#rstrip:刪除右側空白

#lstrip:刪除左側空白
#strip:刪除兩側空白

favorite_language = 
python print (favorite_language) favorite_language = python print (favorite_language.rstrip()) favorite_language = python favorite_language = favorite_language.rstrip() print (favorite_language)

favorite_language =  python 
print (favorite_language.rstrip())
print (favorite_language.lstrip())
print (favorite_language.strip())

Python編程從入門到實踐-第2章-字符串