python isupper函数_python字符串内置函数_Python中的内置字符串函数

python字符串内置函数

For the final portion, we will see some really useful string functions to work with strings in python. Below we have mentioned a few useful string functions.

对于最后一部分,我们将看到一些非常有用的字符串函数,可用于python中的字符串。 下面我们提到了一些有用的字符串函数。

len(字符串) (len(string))

len or length function is used to find the character length of any string. len returns a number and it takes a string as an argument. For Example,

len或length函数用于查找任何字符串的字符长度。 len返回一个数字,并使用字符串作为参数。 例如,

>>> s = "Hello"

>>> print (len(s))

5

5

Live Example →

现场示例→

查找(子字符串) (find(subString))

In case you want to find the position of any character or of a subString within any given string, you can use the find function. It's implementation is a little different than a normal function but it's not tough to understand. Obviously to find a subString in a string, we will have to provide both the main string and the subString to be found, to the funtion. For Example,

如果要查找任何字符或子字符串在给定字符串中的位置,可以使用find函数。 它的实现与普通函数有些不同,但并不难理解。 显然,要在字符串中查找subString,我们必须将主要字符串和要找到的subString提供给该功能。 例如,

>>> s = "Hello"

>>> ss = "He"

>>> print (s.find(ss))

0

0

Since, He is present at the beginning of the string Hello, hence index 0 is returned as the result. It can be directly implemented/used as follows(in case you hate useless typing; which every programmer do):

由于He存在于字符串Hello的开头,因此返回索引0 。 它可以按以下方式直接实现/使用(以防万一您讨厌无用的输入;每个程序员都可以这样做):

>>> print ("Hello".find("He"))

0

0

Live Example →

现场示例→

string_name.lower() (string_name.lower())

lower() function is used to convert all the uppercase characters present in a string into lowercase. It takes a string as the function input, however the string is not passed as argument. This function returns a string as well.

lower()函数用于将字符串中存在的所有大写字符转换为小写。 它使用字符串作为函数输入,但是不会将字符串作为参数传递。 此函数也返回一个字符串。

>>> print ("Hello, World".lower());

hello, world

你好,世界

Live Example →

现场示例→

string_name.upper() (string_name.upper())

upper() is used to turn all the characters in a string to uppercase.

upper()用于将字符串中的所有字符变为大写。

>>> print ("Hello, World".upper());

HELLO, WORLD

你好,世界

Live Example →

现场示例→

string_name.islower() (string_name.islower())

islower() is used to check if string_name string is in lowercase or not. This functions returns a boolean value as result, either True or False.

islower()用于检查string_name字符串是否小写。 该函数返回一个布尔值True或False 。

>>> print ("hello, world".islower())

True

真正

Live Example →

现场示例→

>>> print ("Hello, World".islower());

False

string_name.isupper() (string_name.isupper())

isupper() is used to check if the given string is in uppercase or not. This function also returns a boolean value as result, either True or False.

isupper()用于检查给定的字符串是否为大写。 此函数还返回布尔值True或False 。

>>> print ("HELLO, WORLD".isupper());

True

真正

>>> print ("Hello, World".isupper());

False

Live Example →

现场示例→

string_name.replace(old_string,new_string) (string_name.replace(old_string, new_string))

replace() function will first of all take a string as input, and ask for some subString within it as the first argument and ask for another string to replace that subString as the second argument. For Example,

replace()函数首先将一个字符串作为输入,并要求其中包含一些subString作为第一个参数,并要求另一个字符串替换该subString作为第二个参数。 例如,

>>> print ("Hello, World".replace("World", "India"));

Hello, India

你好,印度

Live Example →

现场示例→

string_name.split(字符,整数) (string_name.split(character, integer))

Suppose you're having a string say,

假设您说的是一个字符串,

>>> mystring = "Hello World! Welcome to the Python tutorial"

Now we can use the split() function to split the above declared string.

现在我们可以使用split()函数拆分上面声明的字符串。

If we choose to split the string into two substring from the exclamation mark !. We can do that by putting an exclamation mark ! in the character argument. It will basically split the string into different parts depending upon the number of exclamation marks ! in the string. All the sub-pieces of the string will be stored in a list. Like,

如果我们选择从感叹号将字符串分成两个子字符串! 。 我们可以通过添加感叹号来做到这一点! 在字符参数中。 基本上会根据感叹号的数量将字符串分成不同的部分! 在字符串中。 字符串的所有子片段都将存储在列表中。 喜欢,

>>> print (mystring.split("!"))

['Hello World', ' Welcome to the Python tutorial']

['Hello World','欢迎使用Python教程']

You can store these values to another variable and access each element of it like this:

您可以将这些值存储到另一个变量,并按如下方式访问它的每个元素:

>>> myNEWstring = mystring.split("!")

>>> print (myNEWstring[0]);

>>> print (myNEWstring[1]);

Hello World Welcome to the Python tutorial

Hello World欢迎使用Python教程

Live Example →

现场示例→

翻译自: https://www.studytonight.com/python/string-functions

python字符串内置函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值