Python_string

字符串在数据的预处理还是很重要的。今天来主要温习一下这部分。
紧跟时代大潮,以下每个小函数的功能都将以 English 介绍之。

Quickly get on the bus!

实例1

  • Function : count the number of vowels
# 计算给出的字符串中,元音字母aeiouAEIOU的个数,区分大小写
s = 0
def vowels(s):
  s = raw_input()
  count = 0
  print('please input some words : ', s)   # 输入s
  if 
  for c in s:  # 循环s,并累加元音字母的个数
    if c in 'aeiouAEIOU':
      count = count + 1
  return count

实例2

  • Function : upper the first letter of every words in our file
#  将文件中的名字,首字母大写,其余字母小写

f = open(file, mode)  
# file要自己根据文件所在路径填写,mode默认就是read,w代表write

for line in f:
    line = line.strip()  # 去掉空格
    print(line.title())  # 首字母大写

f.close()  # remember close your file !!!!!

实例3

  • Function : find and print all the palindrome in our files
def is_panlindrom(name):  # 回文字符
    low = 0  # index of the left letter
    high = len(name) - 1  # index of the right letter

# compare two letters
    while low < high:  # when left letter not equal to right letter
        if name[low] != name[high]:
            return False  # this is not a panlidrom
        low += 1  # move a step to right
        high -= 1  # move a step to left
    return True

f = open(file, mode) 
for line in f:
    line = line.strip()
    if is_panlindrom(line):
        print(line)


f.close()

Attention

  • Assign the new value to the original character when you've already modified your string
  • close the file if you've opened it

The 2nd Edition

正则表达式的故事

正则表达式用来描述字符串的模式

import re  # regular
pattern = 'xxxxx'
re.search(pattern, str)

  • nothing to say... help yourself
    后续会有更新,等我多学关于正则表达式的相关内容的。小朋友我们下次见!

你可能感兴趣的:(Python_string)