python面试之数据类型(字符串)

1.可变数据类型和不可变数据类型

结论:不可变数据类型更改后地址发生改变,可变数据类型更改地址不发生改变

id()是查看内存地址

1.1不可变类型有:Number(数字) String(字符串) Tuple (元组)。


int:

if __name__ == '__main__':
    a = 1
    print(id(a), type(a)) #10914496 <class 'int'>
    
    a = 2
    print(id(a), type(a)) #10914528 <class 'int'>

String:

if __name__ == '__main__':
    b = 'python'
    print(id(b), type(b)) #140361934391424 <class 'str'>
    
    b = 'java'
    print(id(b), type(b)) #140361934392040 <class 'str'>

tuple:

if __name__ == '__main__':
    c = (1, 2, 3)
    print(id(c), type(c)) #139716300990432 <class 'tuple'>
    
    c = (1, 2, 3)
    print(id(c), type(c)) #139716300990504 <class 'tuple'>

1.2可变类型有: List(列表) Dictionary (字典) Sets(集合)。

set:

if __name__ == '__main__':
    s = {1, '2', '3', '4', 5}
    print(s, type(s), id(s)) #{1, 5, '2', '3', '4'} <class 'set'> 140264818849160
    
    s.add('6')
    print(s, type(s), id(s)) #{1, '6', 5, '2', '3', '4'} <class 'set'> 140264818849160

list:

if __name__ == '__main__':
    list = [1, '2', '3', True]
    print(list, type(list), id(list)) #[1, '2', '3', True] <class 'list'> 140427565401672
    
    list.append('4')
    print(list, type(list), id(list)) #[1, '2', '3', True, '4'] <class 'list'> 140427565401672

dic:

if __name__ == '__main__':
    d = {'key1': 1, 'key2': '2', 'key3': '3'}
    print(d, type(d), id(d)) #{'key1': 1, 'key2': '2', 'key3': '3'} <class 'dict'> 140699471033976
    
    d['key4'] = '4'
    print(d, type(d), id(d)) #{'key1': 1, 'key2': '2', 'key3': '3', 'key4': '4'} <class 'dict'> 140699471033976

2.“hello world"转换为首字母大写"Hello World”

capitalize()将字符串的第一个字母变成大写,其他字母变小写

if __name__ == '__main__':
    arrStr = "hello world".split(" ")
    newStr = f"{arrStr[0].capitalize()} {arrStr[1].capitalize()}"
    print(newStr)

3.检测字符串中只含有数字

isdigit() 方法检测字符串是否只由数字组成,返回True or False

if __name__ == '__main__':
    str = "123456";
    print(str.isdigit()) #True
    str = "123456a"
    print(str.isdigit()) #False

4.简单字符串反转的方法

if __name__ == '__main__':
    s = '123456'
    print(s[::-1]) #654321

5.列举几个字符串格式化方式


if __name__ == '__main__':
    #s:获取传入的对象__str__方法的返回值,并将其格式化到指定位置
    s = 'hello, %s' % 'python'
    print(s) #hello, python

    #d:将整数,浮点数转化为十进制表示,并将其格式化到指定位置
    s = 'hello, %s, %d' % ('python', 2020)
    print(s)  #hello, python, 2020

    s = 'hello, %(name)s, %(year)d' % {'name': 'python', 'year': 2020}
    print(s) #hello, python, 2020

    #+:右对齐 -:左对齐
    s = 'hello, %(name)+10s, %(year)-10d!' % {'name': 'python', 'year': 2020}
    print(s) #hello,     python, 2018      !

    #f:保留小数
    s = 'hello, %(name)s, %(year).3f!' % {'name': 'python', 'year': 2020}
    print(s) #hello, python, 2020.000!

    #r:原样输出
    s = 'sunday'
    print("Today is %r" % s) #Today is 'sunday'

    #format方式
    s = 'hello, {}, {}'.format('python', 2020)
    print(s) #hello, python, 2020

    s = 'hello, {0}, {1}, hi, {0}'.format('python', 2020)
    print(s) #hello, python, 2020, hi, python

    s = 'hello, {name}, {year}, hi, {name}'.format(name='python', year=2020)
    print(s) #hello, python, 2020, hi, python

    s = 'hello, {:s}, {:d}, hi, {:f}'.format('python', 2020, 9)
    print(s) #hello, python, 2020, hi, 9.000000

6.自己写怎么去掉字符串前后的空格

import re

def trimStr(str):
    if s.startswith(' ') or s.endswith(' '):
        #\s匹配空白字符
        #()表示分组
        #sub用来替换字符串
        return re.sub(r"^(\s+)|(\s+)$", "", s)

if __name__ == '__main__':
    s = ' A BC '
    print(trimStr(s))

7.获取字符串最后两个字符

if __name__ == '__main__':
    text = 'python'
    #len()获取长度
    print(text[len(text)-2:len(text)+1]) #前闭后开

8.字符GBK转成 UTF-8

import chardet
if __name__ == '__main__':
    str = '字符串'  # UTF-8
    # 解码的话需要指定原来是什么编码
    temp_str_unicode = str.encode('utf-8').decode('utf-8')
    # unicode进行编码
    temp_gbk = temp_str_unicode.encode('gbk')
    print(temp_gbk)
    # 查看什么编码
    print(chardet.detect(temp_gbk))   

9.用正则去掉最后一个空格

if __name__ == '__main__':
    # [\u4e00-\u9fa5]是匹配中文
    str = '你好 中国 '
    pat = re.compile(r'.+[\u4e00-\u9fa5]+')
    result = pat.findall(str)
    print(result[0])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值