Python3.4字符串基础及每次处理一个字符

字符串基础
  1. "" 
  2. Python文本之一[字符串基础] 
  3. Python version: 3.4 
  4. """  
  5.   
  6. #单引号  
  7. str_single_quotes = 'blog: http://www.csdn.net/wirelessqa'  
  8. #双引号  
  9. str_double_quotes = "blog: http://www.csdn.net/wirelessqa"  
  10.   
  11. print ("## 单引号: " + str_single_quotes)  
  12. print ("## 双引号: " + str_double_quotes)  
  13.   
  14. #用\分行  
  15. str_multi_line = "blog:\  
  16. http://www.csdn.net/wirelessqa"  
  17.   
  18. print ("## 使用\\分行: " + str_multi_line)  
  19.   
  20. #用\n换行显示  
  21. str_n = "blog:\nhttp://www.csdn.net/wirelessqa"  
  22.   
  23. print ("## 使用\\n换行: " + str_n)  
  24.   
  25. #三引号"""显示多行  
  26. str_more_quotes = """  
  27. my   
  28. name  
  29. is  
  30. Mr.B  
  31. """  
  32.   
  33. print ("## 使用三引号\"\"\"n显示多行: " + str_more_quotes)  
  34.   
  35. #用r或R显示原貌  
  36. str_r = r"我是\  
  37. 帅哥"  
  38.   
  39. str_R = R"我是\n帅哥"  
  40.   
  41. print ("## 用r显示原貌: " + str_r)  
  42. print ("## 用R显示原貌: " + str_R)  
  43.   
  44. #使用u或U使之成为Unicode字符串  
  45. str_u = u'老\u0020毕'  
  46.   
  47. print ("## 使用u或U使之成为Unicode字符串: " + str_u)  
  48.   
  49. #注意: 字符串是无法改变的,无论你对它做什么操作,你总是创建了一个新的字符串对象,而不是改变了原有的字符串  
  50. #  
  51. #字符串是字符的序列,所以也可以通过索引的方法访问单个字符  
  52.   
  53. test_str_index = "我是帅哥"  
  54.   
  55. print ("## index 0: " + test_str_index[0])  
  56. print ("## index -1: " + test_str_index[-1])  
  57.   
  58. #使用切片访问字任串的一部分  
  59. print ("## [0:3]: " + test_str_index[0:3])  
  60. print ("## [2:]: " + test_str_index[2:])  
  61. print ("## [-1:]: " + test_str_index[-1:])  
  62.   
  63.   
  64. print ("## 遍历整个字符串: ")  
  65. for t in test_str_index:print (t)  
  66.   
  67. #构建另一个序列  
  68. str_list = list(test_str_index) #['我', '是', '帅', '哥']  
  69.   
  70. #字符串拼接  
  71. str_add = test_str_index + '哈哈'  
  72. print ("## 字符串拼接" + str_add)  
  73.   
  74. print("## 使用乘法对字符串多次重复: " + '老毕' * 3)  
  75.   
  76. #使用s.isdigit()来判断字符串是否全为数字   
  77. test_isdigit_true = '782670627'  
  78. test_isdigit_false =  'abcd123'  
  79. test_isdigit_empty = ''  
  80.   
  81. if test_isdigit_true.isdigit():  
  82.     print (test_isdigit_true + " 字符串都是数字")  
  83.   
  84. if not test_isdigit_false.isdigit():  
  85.     print (test_isdigit_false + " 字符串不都是数字")  
  86.   
  87. if not test_isdigit_empty.isdigit():  
  88.     print ("字符串为空")  
  89.   
  90. if len(test_isdigit_empty) == 0:  
  91.     print ("字符串为空")  
  92.   
  93. #将字符串转换成大写  
  94. test_upper = test_isdigit_false.upper()  
  95. print(test_upper)  
  96.   
  97. #将字符串转换成小写  
  98. test_lower = test_upper.lower()  
  99. print(test_lower)  
  100.   
  101. #测试某个字符在字符串中出现的次数  
  102. test_count = "my name is my name"  
  103. print ("## 测试某个字符在字符串中出现的次数: "+ str(test_count.count("name")))  
  104.   
  105. #使用s.splitlines()将一个有多行文本的字符串分隔成多行字符串并入一个列表中  
  106. one_large_str = "chu he ri dang wu, \n han di he xia tu"  
  107. list_lines = one_large_str.splitlines() #['chu he ri dang wu, ', ' han di he xia tu']  
  108. print (list_lines)  
  109.   
  110. #使用'\n'.join()重新生成一个庞大的单字符串  
  111. one_large_str2 = '\n'.join(list_lines)  
  112. print (one_large_str2)  

每次处理一个字符
  1. """ 
  2. Python3.4【文本】之每次处理一个字符 
  3. """  
  4. test_str = "my name is bixiaopeng"  
  5.   
  6. for x in range( 0, len(test_str)-1):  
  7.     print ("## 通过索引遍历字符串: " + test_str[x])  
  8.   
  9. for x in test_str:  
  10.     print ("## 直接遍历字符串: " + x)  
  11.   
  12.   
  13. thelist = list(test_str)  
  14. print (thelist) #['m', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'b', 'i', 'x', 'i', 'a', 'o', 'p', 'e', 'n', 'g']  
  15.   
  16. for x in thelist:  
  17.     print ("## 遍历list: " + x)  
  18.   
  19. def printstr(a):  
  20.     print ("使用map遍历: " + a)  
  21.   
  22. list(map(printstr,test_str)) #注意: python3以后必须使用list来打印map的结果  
  23.   
  24.   
  25. [print ("## 列表推导遍历: " + i) for i in test_str ]  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值