• python基础


    1.1 字符串函数

    字符串可以包含在单引号、双引号、3个单引号、3个双引号之间。

    split() --将一个字符串拆分成一个子字符串列表。

    string1 = "My deliverable is due in may"
    string1_list1 = string1.split()
    string1_list2 = string1.split(" ",2)
    print("Output #11 :{0}".format(string1_list1))
    print("Output #12 :{0}".format(string1_list2))
    string2 = "Your,deliverable,is,due,in,june"
    string2_list = string2.split(',')
    print("Output #13 :{0},{1},{2}".format(string2_list[1],string2_list[5],string2_list[-1]))

    --------------结果

    Output #11 :['My', 'deliverable', 'is', 'due', 'in', 'may']
    Output #12 :['My', 'deliverable', 'is due in may']
    Output #13 :deliverable,june,june

    join() ---将列表中的子字符串合成一个字符串

    print("Output #14 :{0}".format(','.join(string2_list)))

    Output #14 :Your,deliverable,is,due,in,june

    strip() --两侧删除空格、制表符和换行符,lstrip() --左侧删除空格、制表符和换行符,rstrip() --右侧删除空格、制表符和换行符

    string3 = " Remove unwanted character from this string. "
    print("Output #1: string3: {0:s}".format(string3))
    string3_lstrip = string3.lstrip()
    print("Output #2: lstrip: {0:s}".format(string3_lstrip))
    print("Output #3: rstrip: {0:s}".format(string3.rstrip()))
    print("Output #4: strip: {0:s}".format(string3.strip())

    Output #1: string3:  Remove unwanted character  from this string.		   
    
    Output #2: lstrip: Remove unwanted character  from this string.		   
    
    Output #3: rstrip:  Remove unwanted character  from this string.
    Output #4: strip: Remove unwanted character  from this string.

    replace() --将字符串中的一个或一组字符替换为另一个或另一组字符。

    lower(),upper(),capitalize()--lower,upper分别用来将字符串中的字母转换为小写和大写。capitalize将字符串中的第一个字母应用upper函数,其余字母应用lower函数。

    1.2正则表达式与模式匹配

    import re
    string="The quick brown fox jumps over the lazy dog."
    string_list = string.split()
    pattern = re.compile(r"The",re.I)
    count = 0
    for word in string_list:
    if pattern.search(word):
    count += 1
    string_list = string.replace("The","THE")
    print("Output #1 :{0:d}".format(count))
    print(string_list)

       Output #1 :2
    THE quick brown fox jumps over the lazy dog.

    re.compile()将文本形式的模式编译成为编译后的正则表达式。re.I函数确保模式是不区分大小写的,原始字符串r可以确保python不处理字符串的转义字符。

    #在字符串中每次找到模式时将其打印出来 

    import re
    string="The quick brown fox jumps over the lazy dog."
    string_list = string.split()
    pattern = re.compile(r"(?P<match_word>The)",re.I)
    print("Output #2:")
    for word in string_list:
    if pattern.search(word):
    print("{:s}".format(pattern.search(word).group('match_word')))

      

    Output #2:
    The
    the

    #使用字母"a"替换字符串中的单词"the"

    import re
    string="The quick brown fox jumps over the lazy dog."
    string_to_find = r"The"
    pattern = re.compile(string_to_find,re.I)
    print("Output 2#: {:s}".format(pattern.sub("a",string)))

    Output 2#: a quick brown fox jumps over a lazy dog.

    1.3日期

    #!/usr/bin/env python3
    from math import exp,log,sqrt
    import re
    from datetime import date,time,datetime,timedelta
    today = date.today()
    print("{0!s}".format(today))
    print("{0!s}".format(today.year))
    print("{0!s}".format(today.month))
    print("{0!s}".format(today.day))
    current_datetime = datetime.today()
    print("{0!s}".format(current_datetime))
    one_day = timedelta(days=-1)
    yesterday = today + one_day
    print("{0!s}".format(one_day))
    print("{0!s}".format(yesterday))
    eight_hours = timedelta(hours=-8)
    print("{0!s} {1!s}".format(eight_hours.days,eight_hours.seconds))
    date_diff = today - yesterday
    print("{0!s}".format(date_diff))
    print("{0!s}".format(str(date_diff).split()[0]))
    #根据一个日期对象创建具有特定的字符串
    print("{:s}".format(today.strftime('%m/%d/%Y')))
    date1 = today.strftime('%m/%d/%Y')
    print("{!s}".format(datetime.strptime(date1,'%m/%d/%Y')))

    2020-06-17
    2020
    6
    17
    2020-06-17 22:12:06.147000
    -1 day, 0:00:00
    2020-06-16
    -1 57600
    1 day, 0:00:00
    1
    06/17/2020
    2020-06-17 00:00:00


  • 相关阅读:
    微信小程序在扫一扫进入小程序的时候 安卓手机后台继续运行的常规处理
    在微信小程序上,帮助中心界面实现类似手风琴案例
    使用artTemplate的模板引擎,使用简单
    使用原生JavaScript实现图片预加载,方法简单代码少
    在wepy框架中 使用promise对发送网络请求进行封装 包括post跟get请求
    JavaScript实现按字典排序进行md5加密, 以及个人在小程序也可以实现
    当在微信扫一扫进入小程序 并获取到二维码的参数 从而实现扫码进入小程序
    第九章:看看精彩的世界-使用网络技术
    玉渊潭公园
    军事博物馆
  • 原文地址:https://www.cnblogs.com/wei23/p/13152656.html
Copyright © 2020-2023  润新知