Python学习笔记4 - 字符串与循环

什么是字符串?
字符串是以单引号或者双引号括起来的任意文本。
'abc' ''def''
创建字符串:

str1 = "sunk is a good man"
str2 = "sunk is a nice man"
str3 = "sunk is a handsom man"

字符串运算

# 字符串连接 
str6 = "sunck is a"
str7 = "good a man"
print(str6+str7)
# 重复输出字符串
str8 = "good"
print(str8*20)
#返回字符串中的某个字符
#通过索引下标查找字符,索引从0开始
#字符串名[下标]
str11 = "sunck is a good man!"
print(str11[1])
#str11 = "a" 字符串不可变
print("str11 =",str11)

#截取字符串中的一部分
str13 = "sunck is a good man!"
str15 = str13[6:14]
#从给定下标处开始截取到给定下标之前
str12 = str13[:5]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
#格式化输出
print("sunc is a good man ")
num = 10 
print("num =",num)

eval() 方法
功能:将字符串Str当成有效的表达式来求值并返回计算结果

num1 = eval("123")
print(num1)
print(type(num1))
print(evale("+123"))
print(evale("-123"))
print(evale("12+3"))
print(evale("12-3"))

len()
功能: 返回字符串长度

print(len("sunck is a good man凯"))

lower() 转换字符串中大写字母为小写字母

str20 = "SUNCK is a Good Man"
str21 = str20.lower()
print(str21)

uper() 转换字符串的小写字母为大写字母

str21 = "SUNCK is a Good Man"
print(str21.uper)

swapcase() 转换字符串中的小写字母为大写,大写字母为小写

str22 = "SUNCK is a gOOd Man"
print(str22)

capitalize() 句子首字母大写

title() 每个单词的首字母大写

center(width,fillchar)

str25 = "kali is a nice man"
print(str25.center(40,"*")

str26 = "kali is a nice man"
print(str26.rjust(40,"%"))

str28 = "kali is a nice man"
print(str28.zifll(40))# 返回一个长度为width的字符串,原字符右对齐,前面补0

str29 = "kali is a very very nice man"
print(str29.count("very"))#返回字符中str出现的次数,可以指定一个范围。

str30 = "kali is a very very nice man"
print(str30.find("very"))#检测字符串中是否包含在字符串中,可以指定范围,默认从头到尾。得到的是第一次出现的下标,没有返回-1

str31 = "kali is a very very nice man"
print(str31.rindex())#和find()一样呢,只不过如果str不存在时,会抛出异常

str33 = "     kali is a very very nice man"
print(str33.lstrip())# 截掉字符串左侧指定的字符,默认空格
print(str33.rstrip())

http://python.jobbole.com/85122/ 常用的字符串处理函数

while 语句:

while 表达式:
语句

逻辑:当程序执行到whil语句时,首先计算“表达式”的值,如果“表达式”的值为假,那么结束整个循环,为真,执行语句。执行完语句再去计算表达式的值。

打印 1 ~ 5

num = 1
while num <= 5:
  print(num)
  num += 1

计算1+2+3.....+100

sum = 0
num = 1
while num <= 100:
  sum += num
  num += 1
print("sum = %d " % (sum))

打印字符串

str = "sunck is a nanasome man"
index = 0
while index < len(str):
  print("str[%d] = %s" % (index ,str[index]))
  index += 1

字符串比较大小
从第一个字符开始比较,谁的ASCII值大谁就大,如果相等,继续比较下一个字符的ASCLL码

print("azzzz" < "m")

打印出所以的水仙花数

num = 1
while num <= 999:
  a = num % 10         #取出百位
  b = num // 10 % 10   #取出十位
  c = num //100        #取出个位
  
  if num == a**3+b**3+c**3:
    print(num)

打印1~999 所有的质数:

num = 2
while num <= 999:
  if num == 2:
    print("这是质数!")

你可能感兴趣的:(Python学习笔记4 - 字符串与循环)