""" 1.1交换 已知 a的值为”hello”,b的值为”world”,如何交换a和b的值? 得到a的值为”world”,b的值为”hello” """ a = "hello" b = "world" a,b = b,a print(a) print(b) """ 1.2回文 回文的定义:”回文” 就是正读倒读都一样的。 如奇数个:”98789”,这个数字正读是”98789” 倒读也是”98789”。 偶数个数字”3223”也是回文数。 字母 “abcba” 也是回文。 判断一个字符串是否是回文字符串,是打印True, 不是打印False """ def my_func(): x = input("请输入任意字符串: ") if x == x[::-1]: return True else: return False print(my_func()) """ 1.3字符串切割 已知一个字符串为 “hello_world_vivi”, 如何得到一个队列 [“hello”,”world”,”vivi”] """ a = "hello_world_vivi" b = a.split("_") print(b) """ 有个列表 [“hello”, “world”, “vv”]如何把把列表里面的字符串联起来, 得到字符串 “hello_world_vv” """ """ 1、列表里的元素必须都是字符串类型的才可以使用join()拼接 2、返回的是一个字符串 """ a =["hello","world","vv"] print("_".join(a)) #执行结果 :hello_world_vv """ 1.5替换字符 把字符串 s 中的每个空格替换成”%20” 输入:s = “We are happy.” 输出:”We%20are%20happy.” """ s = "We are happy." print(s.replace(" ","%20")) #执行结果 :We%20are%20happy. """ 1.6九九乘法表 代码示例: 打印99乘法表 """ for i in range(1,10): for j in range(1,i+1): print("{}*{}={} ".format(i,j,i*j),end="") print() """ 1.8 统计字符出现的次数 统计字符串“Hello, welcome to my world.” 中字母w出现的次数 统计单词 my 出现的次数 """ s = "Hello,welcome to my world" print(s.count('w')) print(s.count('my')) #执行结果 :w: 2次 ,my :1次 """ 1.9 统计每个字符出现的次数 题目:输入一个字符串str, 输出第m个只出现过n次的字符,如在字符串 gbgkkdehh 中, 找出第2个只出现1 次的字符,输出结果:d """ from collections import Counter m = 2 n = 1 s = "agbgkkdehh" b = Counter(s) print(dict(b)) x = [i for i ,j in dict(b).items() if j==n] print(x[m-1]) """ 1.10 判断字符a含b 判断字符串a=”welcome to my world” 是否包含单词b=”world” """ a = "welcome to my world" if "world" in a: print("True") else: print("False") """ 1.11 查找字符首次出现位置 输出指定字符串A在字符串B中第一次出现的位置,如果B中不包含A,则输出-1 从 0 开始计数 A = “hello” B = “hi how are you hello world, hello vv !” """ A = "hello" B = "hi how are you hello world,hello vv" print(B.find(A)) """ 1.12 查找字符串最后一次出现位置 输出指定字符串A在字符串B中最后出现的位置,如果B中不包含A,则输出-1 从 0 开始计数 A = “hello” B = “hi how are you hello world, hello vv !” """ A = "hello" B = "hi how are you hello world,hello vv" print(B.rfind(A)) """ 1.13判断奇数偶数 给定一个数a,判断一个数字是否为奇数或偶数 a1 = 13 a2 = 10 a = 13 """ a =0 if a % 2 ==0: print("偶数") else: print("奇数") a = 10 print("我是偶数" if a % 2==0 else "我是奇数") """ 1.14判断一个姓名是否姓王 输入一个姓名,判断是否姓王 a = “王五” b = “老王” """ a = "王五" print("姓王" if a[:1]=="王" else "不姓王") a = "老王" if a[:1] =="王": print("姓王") else: print("不姓王") """ 1.15判断是不是数字 如何判断一个字符串是不是纯数字组成 a = “123456” b = “vivi123” """ a = "123456" print("这是一个纯数字" if a.isnumeric() is True else "不是纯数字") b = "vivi123" print("这不是一个纯数字组合" if b.isnumeric() is False else "这是一个纯数字组合") """ 1.16字符串大小写转换 将字符串 a = “This is string example….wow!” 全部转成大写 字符串 b = “Welcome To My World” 全部转成小写 """ a = "This is string example...wow" print(a.upper()) b = "Welcome To My World" print(b.lower()) """ 1.17字符串去掉首尾空格 将字符串 a = “ welcome to my world “首尾空格去掉 """ a = " welcome to my world " print(a.strip()) """ 1.18字符串去掉左边指定空格或字符 将字符串a = “ welcome to my world !”左边的空格去掉 """ a = " welcome to my world !" print(a.lstrip()) """ 1.19字符串去掉右边指定空格或字符 将字符串a = “ welcome to my world ! “右边的空格去掉 """ a = " welcome to my world ! " print(a.rstrip()) """ 1.20 去除字符串里面所有的空格 将字符串a = “ welcome to my world ! “里面的所有空格都去掉 """ a = " welcome to my world ! " print(a.replace(" ","")) """ 1.21字符串去重后排序 s = “ajldjlajfdljfddd”,去重并从小到大排序输出”adfjl” """ s ="ajldjlajfdljfddd" print("".join(sorted(dict.fromkeys(s)))) """ 1.22字符串去重保留顺序 s = “ajldjlajfdljfddd”,去重保留原来的顺序,输出”adfjl” """ s ="ajldjlajfdljfddd" print("".join([x for x in dict.fromkeys(s)])) """ 1.23画菱形 题目 打印出如下图案(菱形): """ #(1,3,5,7,5,3,1) line = 7 lines = (line+1) // 2 print(lines) stars = [x*2+1 for x in range(lines)] + [x*2-1 for x in range(lines-1,0,-1)] print(a) for star in stars: kong = (line - star) // 2 print(" "*kong + "*" * star) """ 1.24 输入一个正整数,判断是几位数 题目 给一个不多于5位的正整数,要求: 一、求它是几位数, 二、逆序打印出各位数字。 a = 12345 """ a = 12345 print(len(str(a))) print(str(a)[::-1])
Python-面试题-字符串(str)
三十六般武艺,七十二般变化,修练出个人品牌并发出光芒