首页 > 编程语言 > 详细

Python---基础----数据类型的内置函数(主要介绍字符串、列表、元组、字典、集合的内置函数)(二)

时间:2019-05-24 15:14:10      阅读:127      评论:0      收藏:0      [点我收藏+]

2019-05-24

--------------------------------

一、

# splitlines()    以换行切割字符串
s = ‘‘‘日照香炉生紫烟\n疑是银河落九天\n飞流直下三千尺‘‘‘
print(s.splitlines())

二、

# join()   将列表按照指定字符串连接
list1 = [‘日照香炉生紫烟‘,‘疑是银河落九天‘,‘飞流直下三千尺‘]
s = ‘*‘.join(list1)
print(s)

三、

# ljust() 指定字符串的长度,内容靠左边
s = ‘abc‘
print(len(s))
print(s.ljust(5, ‘#‘))

四、

# ljust() 指定字符串的长度,内容靠左边,不足的地方用空格填充,默认空格,返回字符串
s = ‘abc‘
print(len(s))
print(s.ljust(5)+‘aa‘)
# center()  指定字符串长度,内容居中,不足的地方用空格填充,默认空格,返回字符串
print(s.center(5, ‘#‘))
# rjust() 指定字符串的长度,内容靠右边,不足的地方用空格填充,默认空格,返回字符串
print(s.rjust(5, ‘#‘))
五、
 
# strip() 去掉左右两边指定字符,默认是去掉空格
# lstrip() 去掉左侧指定字符,默认空格
# rstrip() 去掉右侧指定字符,默认空格
s = ‘      abc     ‘
print(‘- - -‘+s.strip()+‘- - -‘)
print(‘- - -‘+s+‘- - -‘)
s = ‘aaabcc‘
print(s.lstrip(‘a‘))
print(s.lstrip(‘b‘))
print(s.rstrip(‘b‘))
print(s.rstrip(‘c‘))
---------------------------------
六、
# zfill() 指定字符串长度
s = ‘abc‘
print(s.zfill(5))
---------------------------------
七、
# maketrans()  生成用于字符串的映射表
# translate()  进行字符串替换
s = ‘今天晚上我吃的是小炒肉,可好吃了‘
table = s.maketrans(‘小炒肉‘, ‘大白菜‘)
print(table)
print(s.translate(table))
--------------------------------
八、
help(list)
----------------------
Help on class list in module builtins:

class list(object)
|  list(iterable=(), /)

|  Built-in mutable sequence.

|  If no argument is given, the constructor creates a new empty list.
|  The argument must be an iterable if specified.

|  Methods defined here:

|  __add__(self, value, /)
|      Return self+value.

|  __contains__(self, key, /)
|      Return key in self.

|  __delitem__(self, key, /)
|      Delete self[key].

|  __eq__(self, value, /)
|      Return self==value.

|  __ge__(self, value, /)
|      Return self>=value.

|  __getattribute__(self, name, /)
|      Return getattr(self, name).

|  __getitem__(...)
|      x.__getitem__(y) <==> x[y]

|  __gt__(self, value, /)
|      Return self>value.

|  __iadd__(self, value, /)
|      Implement self+=value.

|  __imul__(self, value, /)
|      Implement self*=value.

|  __init__(self, /, *args, **kwargs)
|      Initialize self.  See help(type(self)) for accurate signature.

|  __iter__(self, /)
|      Implement iter(self).

|  __le__(self, value, /)
|      Return self<=value.

|  __len__(self, /)
|      Return len(self).

|  __lt__(self, value, /)
|      Return self<value.

|  __mul__(self, value, /)
|      Return self*value.

|  __ne__(self, value, /)
|      Return self!=value.

|  __repr__(self, /)
|      Return repr(self).

|  __reversed__(self, /)
|      Return a reverse iterator over the list.

|  __rmul__(self, value, /)
|      Return value*self.

|  __setitem__(self, key, value, /)
|      Set self[key] to value.

|  __sizeof__(self, /)
|      Return the size of the list in memory, in bytes.

|  append(self, object, /)
|      Append object to the end of the list.

|  clear(self, /)
|      Remove all items from list.

|  copy(self, /)
|      Return a shallow copy of the list.

|  count(self, value, /)
|      Return number of occurrences of value.

|  extend(self, iterable, /)
|      Extend list by appending elements from the iterable.

|  index(self, value, start=0, stop=9223372036854775807, /)
|      Return first index of value.
|     
|      Raises ValueError if the value is not present.

|  insert(self, index, object, /)
|      Insert object before index.

|  pop(self, index=-1, /)
|      Remove and return item at index (default last).
|     
|      Raises IndexError if list is empty or index is out of range.

|  remove(self, value, /)
|      Remove first occurrence of value.
|     
|      Raises ValueError if the value is not present.

|  reverse(self, /)
|      Reverse *IN PLACE*.

|  sort(self, /, *, key=None, reverse=False)
|      Stable sort *IN PLACE*.

|  ----------------------------------------------------------------------
|  Static methods defined here:

|  __new__(*args, **kwargs) from builtins.type
|      Create and return a new object.  See help(type) for accurate signature.

|  ----------------------------------------------------------------------
|  Data and other attributes defined here:

|  __hash__ = None
-----------------------------
九、
#  append    向列表末尾添加新元素   返回值None
list1 = [1,2,3,4,5]
print(list1.append(5))
----------------------------
十、
# count() 计算某个元素在列表中出现的次数
list1 = [1,1,2,5,1,3]
print(list1.count(5))
----------------------------
十一、
# extend() 将一个列表继承另一个列表
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
list3 = list1.extend(list2)
print(list1)
print(list2)
print(list3)
print(list1 + list2)
------------------------------
十二、
 

Python---基础----数据类型的内置函数(主要介绍字符串、列表、元组、字典、集合的内置函数)(二)

原文:https://www.cnblogs.com/niaocaizhou/p/10918065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!