首页 > 其他分享 >字符串常用函数

字符串常用函数

时间:2023-02-12 00:34:01浏览次数:38  
标签:count 常用 abc False 函数 print 字符串 word True

供自己查询使用,随时补充

常用函数

  1. 切片
#[start:end:step] 左闭右开[start, end)
s = "Hello word"
print(s[1:5]) # ello 默认 step = 1
print(s[6:]) #word [satrt - 最后]
print(s[:5]) #Hello [最开始, end)
print(s[::-1]) # drow olleH step = -1 倒序
  1. 查找
s = "abc"
print(s.find('a')) #0, 返回下标值,如果没找到返回-1
print(s.find(("bc")))#1, 返回第一个下标值
print(s.index("bc"))#1, 返回第一个下标值,如果没找到抛异常
  1. 拼接,循环去除所有值用x拼接
s = "abc"
print(s.join('1234')) #1abc2abc3abc4
# 常见用法
print(" ".join(["hello","word","this","is","python"])) # hello word this is python
  1. 分割
s = "a b c"
print(s.split(' ')) #['a', 'b', 'c']
  1. 计数
s = "aabbcabdbcc"
print(s.count('a')) #3
print(s.count('x')) #0
  1. 替换
# replace(old, new, count) 把原字符串中的old,替换为new,count表示替换的个数,不写count默认全部替换
s = "abcdefabcd"
print(s.replace("bcd","123"))# a123efa123
print(s.replace("bcd","123",1))# a123ef
  1. 是否以xx开始,或是否以xx结束
s = "abc"
print(s.startswith('a')) # True
print(s.startswith("ac")) # False
print(s.endswith('b')) # False
print(s.endswith("bc")) # True
  1. 首字母大写
s = "abc"
print(s.capitalize()) # Abc
  1. 判断大小写
s = "abcABC"
print(s.islower()) # False
print(s.isupper()) # False

s = "abc"
print(s.islower()) # True

s = "ABC"
print(s.isupper()) # True
  1. 转换成大写/小写
s = "aBc"
print(s.lower()) # abc
print(s.upper()) # ABC
  1. 是否是数字和字母
s = "123abc"
print(s.isalnum()) #True, 是数字和字母
print(s.isdigit()) #False, 是否是数字
print(s.isalpha()) #False, 是否是字母

s = "123"
print(s.isdigit()) #True

s = "abc"
print(s.isalpha()) #True
  1. 去除空格
s = "  abc   "
print(s.strip()) #abc
print(s.lstrip()) #去除左空格 
print(s.rstrip()) #去除右空格

标签:count,常用,abc,False,函数,print,字符串,word,True
From: https://www.cnblogs.com/chenjq12/p/17113138.html

相关文章

  • C++匿名函数
    #include<iostream>std::stringaaa="111";intmain(){//[]称为lambda捕获列表,它的含义为:告诉编译器需要在匿名函数内部使用外部的变量。捕获列表中的内容......
  • [学习笔记] CentOS + .Net后端常用的中间件工具安装
    Redis5.0+官方文档:https://redis.io/download/#redis-downloadssudoyuminstallredisRabbitMQ3.7.11+官方文档:https://www.rabbitmq.com/install-rpm.html配置安......
  • day11- 20.有效括号|1047.删除字符串中所有相邻重复项
    20.有效括号leetcode题目链接:https://leetcode.cn/problems/valid-parentheses/题目描述:给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字......
  • STL-常用算法总结
    算法主要由头文件<algorithm>,<functional>,<numeric>组成<algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、删除等<numeric>体积很小,......
  • STL-常用算法总结
    算法主要由头文件<algorithm>,<functional>,<numeric>组成是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、删除等体积很小,只包括几个在序列上面......
  • fusion app 常用小技巧
    声明:文章来源于网络,如有侵权,请联系删除网页即将加载--网页即将加载if(网页链接:find"url/.")then停止加载()进入子页面("游览",{链接=网页链接})end加载本地......
  • Git常用命令速查表
    用户信息配置#全局配置用户名字和邮箱gitcongfiguser.name"xxxx"--globalgitconfiguser.email"[email protected]"-global查看不同作用域的配置信息gitconfig--lis......
  • git专题-常用命令
    一、常用命令gitconfig--globaluser.name"yyy"#配置用户名gitconfig--globaluser.email"[email protected]"#配置邮件gitconfig--globalcolor.uitrue#g......
  • C语言:字符串的拆分
     1#include<io_utils.h>2#include<string.h>3#include<stdlib.h>45intmain(){6charstring[]="C,1972;C++,1983;Java,1995;Rust,201......
  • C语言学习:字符串的长度与比较
     1#include<io_utils.h>2#include<stdlib.h>3#include<errno.h>4#include<time.h>5#include<string.h>67voidSwapString(char**first,ch......