Python String Methods

str.capitalize()  # 将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境
>>> ‘hello‘.capitalize()
‘Hello‘
>>> ‘hEllo‘.capitalize()
‘Hello‘
string.center(width[, fillchar]) # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格
>>> ‘hello‘.center(10,‘0‘)
‘00hello000‘
>>> ‘hello‘.center(10)
‘  hello   ‘
string.count(sub, start= 0,end=len(string)) #方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置
>>> ‘hello world‘.count(‘o‘)
2
>>> ‘hello world‘.count(‘o‘,4,8)
2
>>> ‘hello world‘.count(‘o‘,4,7)
1
时间: 2024-10-22 06:22:23

Python String Methods的相关文章

Python String Methods 2

1. Python isalnum()方法  #检测字符串是否由字母和数字组成 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False >>> 'hello'.isalnum() True >>> 'hello123'.isalnum() True >>> 'hello_123'.isalnum() False >>> 'this is string'.isalnum() False >

Python String Methods 3

Python ljust()方法 --rjust())#返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串.如果指定的长度小于原字符串的长度则返回原字符串 str.ljust(width[, fillchar]) >>> ' hello world'.ljust(20) ' hello world ' >>> ' hello world'.ljust(20,'=') ' hello world=====' Python lower()方法 --Python up

String methods

A method is similar to a function – it takes arguments and returns a value – but the syntax is different. For example, the method upper takes a string and return a new string with all uppercase letters. Instead of the function syntax upper(text), it

Python string objects implementation

http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects implementation June 19, 2011 This article describes how string objects are managed by Python internally and how string search is done. PyStringObject structu

The internals of Python string interning

JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A few days ago, I had to explain to a colleague what the built-in function intern does. I gave him the following example: >>> s1 = 'foo!' >>>

Python string interning原理

原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何工作的,代码基于CPython2.7.7这个版本. 前一段时间,我向同事解释了python的buil-in函数 intern背后到底做了什么.我给他看了下面这个例子: >>> s1 = 'foo!' >>> s2 = 'foo!' >>> s1 is s2

python string

string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" python string2 " >>> s=s1+s2 >>> s 'python string python string2 ' >>> >>> cmp(s1,s2) 1 string 截取 >>> s1[0

Python String startswith() Method

一,摘自官方API  https://docs.python.org/3/library/stdtypes.html#methods str.startswith(prefix[, start[, end]]) Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, t

python string操作

#!/bin/python #-*- coding=utf-8 -*- import string print("hello,world") str1 = "       python是动态语言       " #打印str1原型 print(str1) #打印去掉两边空格 print(str1.strip()) #字符串大小写转换 str2="abcd EFG,this is a TEST" print(str2.lower()) #小写 pr