python入门(2)
字符串方法
def capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> str
首字母大写
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
"""
return ""
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
def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.endswith(suffix[, start[, end]]) -> bool
以suffix结束,返回true
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
def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int
查找sub第一次出现的下标,找到返回下标的值,找不到返回-1
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 index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(sub[, start[, end]]) -> int
查找sub第一次出现的下标,找到返回下标的值,找不到报错ValueError
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
def isalnum(self): # real signature unknown; restored from __doc__
"""
S.isalnum() -> bool
字符串是字母或数字组成,返回true
Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
"""
return False
def isalpha(self): # real signature unknown; restored from __doc__
"""
S.isalpha() -> bool
纯字母返回true
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
"""
return False
def isdigit(self): # real signature unknown; restored from __doc__
"""
S.isdigit() -> bool
纯数字返回true
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
"""
return False
def islower(self): # real signature unknown; restored from __doc__
"""
S.islower() -> bool
全部都是小写,返回true
Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwise.
"""
return False
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 lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str
全部字母小写
Return a copy of the string S converted to lowercase.
"""
return ""
def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str
替换单词,可设置次数
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
"""
return ""
def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rfind(sub[, start[, end]]) -> int
右查找,返回第一次出现的下标,找不到返回-1
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
def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rindex(sub[, start[, end]]) -> int
右查找,返回第一次出现的下标,找不到报错ValueError
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.
Raises ValueError when the substring is not found.
"""
return 0
def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.split(sep=None, maxsplit=-1) -> list of strings
以sep切分字符串,返回链表
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 []
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 ""
def title(self): # real signature unknown; restored from __doc__
"""
S.title() -> str
将每个单词首字母大写
Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case.
"""
return ""
def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str
字母全部大写
Return a copy of S converted to uppercase.
"""
return ""
####upper,title,capitalize区分
str1 = "hello world"
print(str1)
print(str1.capitalize())
print(str1.title())
print(str1.upper())
# 结果
# hello world
# Hello world
# Hello World
# HELLO WORLD
列表
列表
[]
练习:
-
l=[1,1,6,3,1,5,2] ,去重,至少两种方法
方法一:
l=[1,1,6,3,1,5,2] news_ = [] for i in l: if i not in news_: news_.append(i) print(news_) #[1, 6, 3, 5, 2]
方法二:
l=[1,1,6,3,1,5,2] new_=list(set(l)) print(new_) #[1, 2, 3, 5, 6]
set 一个不重复的元素组
-
实现字符串反转 输入str="string"输出’gnirts’ 三种方法
方法一:
str_="string" list_=list(str_) list_.reverse() new_="" new_=new_.join(list_) print(new_)
方法二:
str_="string" str_=str_[::-1] print(str_)
方法三:
str_="string" def func(s): l = list(s) result = "" while len(l)>0: result += l.pop() return result new_ = func(str_) print(new_)
-
一行代码实现对列表a中的偶数位置的元素进行加3后求和
l=[1,2,3,4,5,6] print(sum(map(lambda x:x+3,l[1::2]))) # 21
-
List = [-2, 1, 3, -6],如何实现以绝对值大小从小到大将 List 中内容排序
List = [-2, 1, 3, -6] List.sort(key=lambda x:abs(x)) print(List) #[1, -2, 3, -6]