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 uses the method syntax text.upper().

This form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, text. The parentheses indicate that this method has no parameters.

A method call is called an invocation; in this case, we would say that we are invoking upper on the word.

As it turns out, there is a string method named find that is remarkably similar to the function we wrote. Actually, the find method is more general than our function: it can find substrings, not just characters:

It can take as a second argument the index where it should, and the third argument the index where it should end:

from Thinking in Python

String methods

时间: 2024-10-09 12:08:32

String methods的相关文章

Python String Methods

str.capitalize() # 将字符串的第一个字母变成大写,其他字母变小写.对于 8 位字节编码需要根据本地环境 >>> 'hello'.capitalize() 'Hello' >>> 'hEllo'.capitalize() 'Hello' string.center(width[, fillchar]) # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串.默认填充字符为空格 >>> 'hello'.center(10,

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

Python - Methods

call a method using a period . struture like: <objectName>.<methodName>(list of arguments, if any) # Reversing a list L = [1, 2, 3, 4, 5] L.reverse() print(L)f [5, 4, 3, 2, 1] # Some String Methods print('piece of cake'.startwith('pie')) print

python中string模块各属性以及函数的用法

任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 字符串属性函数 系统版本:CentOS release 6.2 (Final)2.6.32

python字符串操作函数和string模块代码分析

原文链接:http://blog.chinaunix.net/uid-25992400-id-3283846.html python的字符串属性函数 字符串属性方法: >>> str='string learn' >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute_

String constants

官方https://docs.python.org/3.4/library/string.html#string.Formatter 6.1. string — Common string operations Source code: Lib/string.py See also Text Sequence Type — str String Methods 6.1.1. String constants The constants defined in this module are: st

Python 字符串操作及string模块使用

python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对齐 1 2 3 4 5 6 7 8 9 10 11 >>> str = "Python stRING" >>> print str.center(20)       #生成20个字符长度,str排中间    Python stRING        &g

Python 笔记 #04# Methods

源:DataCamp datacamp 的 DAILY PRACTICE  + 日常收集. Methods String Methods List Methods 缺一 Methods You can think of methods as functions that "belong to" Python objects. String Methods # string to experiment with: room room = "poolhouse" # U