python基础-day04

本文介绍了Python中字符串和列表的基本操作方法,包括格式化输出、去除空白字符、字符串分割与连接、列表的增删改查等功能,并通过具体示例进行说明。

昨日内容,补充,

格式化输出%r,在哪呢?

自己在网上找了点,简单说就是可以忽略特殊字符,把他当作字符来处理,

string = "Hello\tWill\n"
print("%s" %string)
print("%r" %string)
name = r'|\thf'
name2 = '|\thf'
print(name)
print(name2)
#输出结果
Hello	Will

'Hello\tWill\n'
|\thf
|	hf

whitespace removed,白话就是空格,换行符,tab等,

    def strip(self, chars=None): # real signature unknown; restored from __doc__
        """
        S.strip([chars]) -> str
        
        Return a copy of the string S with leading and trailing
        whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""

strip()

    def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
        """
        S.rsplit(sep=None, maxsplit=-1) -> list of strings
        
        Return a list of the words in S, using sep as the
        delimiter string, starting at the end of the string and
        working to the front.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified, any whitespace string
        is a separator.
        """
        return []

从右边开始分隔,


        isdigit() 全都是数字组成
        isalpha() 全部由字母组成

        isalnum() 全部由数字或者字母组成

alpha:n. 希腊字母的第一个字母;开端;最初

digit:n. 数字;手指或足趾;一指宽

alnum:网络释义:字母和数字


作业讲解:

name = "aleX leNb"
print(name.strip('aN')) #leX leNb
print(name.strip('ab')) #leX leN
print(name.strip('abq')) #leX leN
print(name.lstrip('a').rstrip('b')) #leX leN,这是我自己写的,
#注意看第二种,

content = input("请输入内容:").strip().split('+') 

#题目是加法运算,值得学习的地方是,没事strip一下,清除用户无意间输入的空格。


计算用户输入的内容中有几个整数(以个位数为单位)。

我自己算的没有限定个位数,之前有越界的问题,现在ok了。

content = input("input:")
n = len(content)
m = 0
ls = []
s = ''
while m < n:
    if content[m].isdigit():
        s = s+content[m]
        if (m+1== n) or(not content[m+1].isdigit()) :
            ls.append(int(s))
            s = ''
        m+=1

    else:
        m+=1

for x in ls:
    print(x)


list,列表,也遵循,索引,切片,步长的规则,

增加!!!

def append(self, p_object):     #在末尾增加

def insert(self, index, p_object):     #插入下标index处,原来index以及后面位置数据均后移,

def extend(self, iterable):    #将可迭代对象内元素,依次加入调用对象,

删除!!!

def pop(self, index=None):    #默认删除最后一个,也可自己指定,

def remove(self, value):    #根据值来删除,

def clear(self):        #清空所有元素,

del list        #直接删除列表,再次调用列表会提示未定义

按照切片去修改,修改的新值可能超过了你给的范围,仍然ok,看下面的例子,

li = [ 1, 'a', 'b', 'a', 2, 3,'老男孩']
li[:3] = 'YYYYYYYY'
print(li)
#['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'a', 2, 3, '老男孩']

def sort(self, key=None, reverse=False):     #可正向排序,也可反向排序,默认是从小到大,字符的数字也ok,

def reverse(self):    #就是逆序。

元组:tuple,首先不能修改,其它的比如切片,下标,索引都ok,但是元组中的列表等可变元素可修改。

tu1 = (1,2,'alex',[1,'taibai'],(1,2,3),'oldboy')
tu1[3].append('日天')
print(tu1)
#输出结果
#(1, 2, 'alex', [1, 'taibai', '日天'], (1, 2, 3), 'oldboy')

join和split,

    def join(self, iterable): # real signature unknown; restored from __doc__
        """
        S.join(iterable) -> str
        
        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""


    def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
        """
        S.split(sep=None, maxsplit=-1) -> list of strings
        
        Return a list of the words in S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are
        removed from the result.
        """
        return []
s = '空格 空格 空格'
l = s.split()
print(l)
s1 = ' '.join(l)
print(s1)
#运行结果
# ['空格', '空格', '空格']
# 空格 空格 空格


range(),支持范围,步长,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值