python基础课程第二天学习内容

4.2.2 字符串的查找与检查

4.2.2.1 index()字符串的查找方法

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    S.index(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found, 
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Raises ValueError when the substring is not found.
    """
    return 0
注意点:
1、如果能够在S中找到字符串,就返回最低索引
2、如果在S中找不到字符串,就报错ValueError

Raises 在python中表示抛出一个错误

4.2.2.2 find()和rfind()字符串的查找方法

def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    S.find(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.
    """
    return 0

def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    S.rfind(sub[, start[, end]]) -> int
    
    Return the highest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.
    """
    return 0

区别在与find()是返回字符串中最低索引,而rfind()是最高索引

但是如果返回-1则都表示在字符串中无法找到

4.2.2.3 count()的统计用法

def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    S.count(sub[, start[, end]]) -> int
    
    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.
    """
    return 0

返回在设定范围内的子字符串在字符串中非重叠出现过的次数
如果查找不到会返回0,也可以用作简单的查找

4.2.3 判断是否由全由**组成

isalpha()

def isalpha(self, *args, **kwargs): # real signature unknown
    """
    Return True if the string is an alphabetic string, False otherwise.
    
    A string is alphabetic if all characters in the string are alphabetic and there
    is at least one character in the string.
    """
    pass
判断字符串是否全部由字符组成
返回一个bool类型的数据

isdigit()

def isdigit(self, *args, **kwargs): # real signature unknown
    """
    Return True if the string is a digit string, False otherwise.
    
    A string is a digit string if all characters in the string are digits and there
    is at least one character in the string.
    """
    pass
判断字符串是否全由数字组成
返回一个bool类型的数据

endswith()

def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
    """
    S.endswith(suffix[, start[, end]]) -> bool
    
    Return True if S ends with the specified suffix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    suffix can also be a tuple of strings to try.
    """
    return False
判断结尾是否由给定的字符串结尾
返回一个bool类型的数据

isalnum()

def isalnum(self, *args, **kwargs): # real signature unknown
    """
    Return True if the string is an alpha-numeric string, False otherwise.
    
    A string is alpha-numeric if all characters in the string are alpha-numeric and
    there is at least one character in the string.
    """
    pass
判断是否只由字母或数字组成
返回一个bool类型的数据

join()

def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
    """
    Concatenate any number of strings.
    
    The string whose method is called is inserted in between each given string.
    The result is returned as a new string.
    
    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
    """
    pass
将给定的子字符串添加到字符串的后面,可以做合并操作
返回一个字符串

replace()

def replace(self, *args, **kwargs): # real signature unknown
    """
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    If the optional argument count is given, only the first count occurrences are
    replaced.
    """
    pass
返回一个复制版本字符串,并将old全部替换成new
如果给定了替换次数,则将前几次的old替换成new

strip()

def strip(self, *args, **kwargs): # real signature unknown
    """
    Return a copy of the string with leading and trailing whitespace remove.
    
    If chars is given and not None, remove characters in chars instead.
    """
    pass
删除字符串前后的空格
但是当字符串前后没有空格是它会自动在前后各加上一个空格
返回一个字符串

title()

def title(self, *args, **kwargs): # real signature unknown
    """
    Return a version of the string where each word is titlecased.
    
    More specifically, words start with uppercased characters and all remaining
    cased characters have lower case.
    """
    pass
每个单词首字母大写,返回一个字符串

5 列表

5.1 列表的表达形式

list

from collections import Iterable

print(isinstance(list2,Iterable))
print(isinstance('abc',Iterable))
print(isinstance(123,Iterable))

注意点:

1、列表表达形式是一个:[];还可以用list()

2、列表里面的元素可以使是不同的数据类型

3、列表可以嵌套列表

4、列表是可迭代的(字符串也是可迭代对象)

5.2判断是否是可迭代对象

isinstance(a,b)

可以用来判断前者是否是后者的子类

5.3 迭代列表、字符串(for循环)

可以迭代的对象是可以使用for语句循环的

如果是可迭代对象

list1=[1,2,3,4,5,6,7]

for i in 'abc':
    print(i)

for j in list1:
    print(j)

5.4列表的切片

注意点:

和字符串的切片相似,遵循左闭右开原则

print(list1[0])
print(list1[:])
print(list1[::1])
print(list1[::-1])
print(list1[0:2])
print(list1[-2:])

作业:
第一题

 #方法一
    i=[1,1,6,3,1,5,2]
    i.sort()
    i.pop(0)
    i.pop(0)
    print(i)
#方法二
    j=[1,1,6,3,1,5,2]
    j.sort()
    
    b=len(j)-1
    print(b)
    
    while b>0:
    
        if j[b] == j[b-1]:
            print(j[b])
            print(j[b - 1])
            j.pop(b)
        b=b-1
    
    print(j)

第二题:

#方法一
str1 = 'string'

str2=str1[::-1]
print(str2)
#方法二
str3=str1[:]
list1=list(str3)
while list1:
    print(list1.pop(),end='')
print('')
#方法三
str4=str1[:]
a=len(str4)-1
while a>=0:
    print(str4[a],end='')
    a=a-1

print('')

第三题:

#给出列表
list1 = [1,2,3,4,5,6,7,8,9]
list2 = list1[::2]
#检查列表2是否符合要求
print(list2)

a=0
#使用for循环进行运算
for i in list2:
    a = a+i+3

#输出结果
print(a)

第四题:

#给出列表
list = [-2,1,3,-6]

#先将列表内数据绝对值操作
list1 = list[:]

a = len(list1)-1
b = 0
while a >= b:
    if list1[b] < 0:
        list1[b] = abs(list1[b])
        print(list1[b])
        #检查是否正确的去符号
    b=b+1

list1.sort()
print(list1)
c=0
while a >= c:

    for i in list:
        if list1[c] == abs(i):
            list1[c] = i
            print(list1[c])

    c=c+1
print(list1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值