python变量字符串_Python字符串变量类型

python变量字符串

python变量字符串

The string is a type used to hold text data in Python programming language. We can hold name, surname, address, text or similar data in strings. There are different functions that can be used with these string data. In this tutorial, we will look at them in detail.

字符串是一种用于以Python编程语言保存文本数据的类型。 我们可以将名称,姓氏,地址,文本或类似数据保存在字符串中。 这些字符串数据可以使用不同的功能。 在本教程中,我们将详细介绍它们。

定义字符串变量 (Define String Variable)

Defining string is easy as just setting string value into a variable by using quotes. In this example, we create a string variable named s and set string value This is a string by using a single quote.

定义字符串很容易,因为只需使用引号将字符串值设置为变量即可。 在此示例中,我们创建一个名为s的字符串变量,并设置字符串值。 This is a string通过使用单引号引起This is a string

s='This is a  string'

访问字符 (Access Characters)

String variables consist of characters. If we need we can access these characters like a character array. We will specify start and end index numbers like a list and we will get related characters. In this example, we will get the first 3 characters by giving 0:3 like below.

字符串变量由字符组成。 如果需要,我们可以像字符数组一样访问这些字符。 我们将像列表一样指定开始和结束索引号,并获得相关的字符。 在此示例中,我们将通过给出0:3来获得前3个字符,如下所示。

s[0:3]

s[3:4]

s[3:]
Access Characters
Access Characters
访问字符

更新字符串(Update String)

We can update a string variable by reassigning new values. In this example, we will set the string variable s value as This is a new string by using a double quote.

我们可以通过重新分配新值来更新字符串变量。 在此示例中,我们将使用双引号将字符串变量s值设置为This is a new string

s="This is a new string"

单引号 (Single Quote)

We can use a single quote to define string variables while providing string value. Using a single quote for small symbol-like strings is the best way.

我们可以在提供字符串值的同时使用单引号定义字符串变量。 最佳方法是对像符号一样的小字符串使用单引号。

s='TR'

双引号 (Double Quote)

The double quote can be used in strings where interpolations and natural language messages are required. We can also use a single quote as a string part inside the double quote.

双引号可用于需要插值和自然语言消息的字符串中。 我们还可以将单引号用作双引号内的字符串部分。

s="I'll call you when I'm available"

三重报价 (Triple Quote)

The triple quote is most useful for docstrings and raw string literals definition. The triple quote may wrap multiple lines like below.

三重引号对于文档字符串和原始字符串文字定义最有用。 三重引号可能会包裹多行,如下所示。

sss='''This 
is 
a 
string'''

转换为大写 (Convert to Uppercase)

To make whole string uppercase use the upper function of the string variable.

要使整个字符串大写,请使用string变量的upper函数。

ss="This is poftut"
ss.upper()
Convert Uppercase
Convert Uppercase
转换大写

转换为小写(Convert to Lowercase)

To make uppercase chars use lower() function.

要使用大写字符,请使用lower()函数。

sl="THIS IS POFTUT"
sl.lower()
Convert Lowercase
Convert Lowercase
转换小写

检测字符串类型方法(Detect String Type Methods)

Python provides methods to detect string type. What I mean with string type is for example if the string has numeric characters or uppercase characters etc.

Python提供了检测字符串类型的方法。 我所说的字符串类型是例如,如果字符串具有数字字符或大写字符等。

LEARN MORE  Shell Scripting Languages Examples, Bash, Sh, Python, Powershell, MSDOS, PHP, Tcl, Perl
了解更多Shell脚本语言示例,Bash,Sh,Python,Powershell,MSDOS,PHP,Tcl,Perl

检查数值 (Check Numeric)

We will check the variable type with  isnumeric() function.

我们将使用isnumeric()函数检查变量类型。

>>> num="8" 
>>> print(num.isnumeric()) 
True
>>> num="c" 
>>> print(num.isnumeric()) 
False

检查Alpha (Check Alpha)

We will check the variable type with  isalpha() function.

我们将使用isalpha()函数检查变量类型。

>>> a="poftut1" 
>>> print(a.isalpha()) 
False 
>>> a="poftut"         
>>> print(a.isalpha()) 
True

检查字母数字 (Check Alphanumeric)

We will check the variable type with  isalnum() function.

我们将使用isalnum()函数检查变量类型。

>>> a="poftut1" 
>>> print(a.isalnum())    
True
>>> a="!"              
>>> print(a.isalnum()) 
False

小写 (Lower Case)

We will check variable type with  islower() function.

我们将使用islower()函数检查变量类型。

>>> a="poftut" 
>>> print(a.islower()) 
True 
>>> a="Poftut"         
>>> print(a.islower()) 
False

大写 (Upper Case)

We will check the variable type with  isupper() function.

我们将使用isupper()函数检查变量类型。

>>> a="Poftut"         
>>> print(a.isupper()) 
False 
>>> a="POFTUT"         
>>> print(a.isupper()) 
True

检查空字符串 (Check Empty String)

We will check variable type with  isspace() function.

我们将使用isspace()函数检查变量类型。

>>> a="POFTUT IS"      
>>> print(a.isspace()) 
False 
>>> a="  "             
>>> print(a.isspace()) 
True

连接字符串 (Joining String)

Two strings can be joined together. The first string will be joined for each char in the second string as we will see next example.

两个字符串可以连接在一起。 我们将在下一个示例中看到,第一个字符串将为第二个字符串中的每个字符连接。

>>> a="I love poftut" 
>>> " ".join(a) 
'I   l o v e   p o f t u t'

倒弦 (Reversing String)

Reversing string can be done with reversed() function. But there is a tip here. Use reversed with join because reversed returns iterator which is not a string. By using it with join with null string value we will get reversed string.

反转字符串可以使用reversed()函数完成。 但是这里有一个提示。 与join一起使用reversed,因为reversed返回的迭代器不是字符串。 通过将其与具有空字符串值的连接一起使用,我们将获得反向字符串。

>>> a="I love poftut"
>>> print("".join(reversed(a))) 
tutfop evol I

分割字符串 (Splitting String)

The splitting string is easy. By default, space is used as a delimiter. But delimiter can be provided.

分割字符串很容易。 默认情况下,空格用作定界符。 但是可以提供定界符。

>>> a.split() 
['I', 'love', 'poftut'] 
>>> a.split('o') 
['I l', 've p', 'ftut']

替换字符串 (Replacing String)

Replace function gets two parameters first is which chars will be changed, second is what will be new chars.

Replace函数有两个参数,第一个是要更改的字符,第二个是新的字符。

>>> a="I love poftut"           
>>> a.replace("poftut","POFTUT")
I love POFTUT

大写 (Capitalize)

We can capitalize on the start character or first letter of the string by using capitalize function.

通过使用capitalize功能,我们可以利用字符串的起始字符或首字母。

s="this is a string"

s.capitalize()
Capitalize
Capitalize
大写

计数发生(Count Occurrence)

String variable provides count function to get the count of given characters occurrence. In this example, we will count i .

字符串变量提供count功能来获取给定字符出现的计数。 在此示例中,我们将计算i

s="this is a string" 
s.count('i')
Count Occurrence
Count Occurrence
计数发生

带状空间(Strip Spaces)

While working with strings there will be some unwanted spaces at the begging or end of the strings. We can remove the spaces easily with split function.

在使用琴弦时,在琴弦的开头或结尾会有一些多余的空格。 我们可以使用split函数轻松删除空格。

LEARN MORE  How To Check If String Contains A Specific Word in PHP?
了解更多如何在PHP中检查字符串是否包含特定单词?

s=”  this is a string  ” s.strip()

s =”这是一个字符串” s.strip()

Strip Spaces
Strip Spaces
带状空间

将字符串拆分为单词列表(Split String Into Word List)

Another useful function provided by a string is splitting the string into a word list. While slitting space will be used as a delimiter.

字符串提供的另一个有用功能是将字符串拆分为单词列表。 同时分割空间将用作定界符。

s="this is a string"     
word_list = s.split()
Split String Into Word List
Split String Into Word List
将字符串拆分为单词列表

Python字符串变量类型信息图(Python String Variable Type Infographic)

Python String Variable Type Infographic
Python String Variable Type Infographic
Python字符串变量类型信息图

翻译自: https://www.poftut.com/python-string-variable-type/

python变量字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用单引号或双引号来声明字符串变量。例如: ``` str1 = 'Hello World' str2 = "I love Python" ``` 你还可以使用三引号来声明多行字符串变量,例如: ``` str3 = '''This is a multi-line string in Python''' ``` 此外,你还可以使用字符串格式化和字符串替换来操作字符串字符串格式化可以使用`format()`方法,例如: ``` name = "Alice" age = 25 print("My name is {} and I am {} years old.".format(name, age)) ``` 字符串替换可以使用`replace()`方法,例如: ``` sentence = "I love Java" new_sentence = sentence.replace("Java", "Python") print(new_sentence) ``` 还有一些其他的字符串操作方法,比如删除空白字符。你可以使用`strip()`方法来删除字符串两侧的空白字符,或使用`lstrip()`和`rstrip()`方法来删除字符串左侧和右侧的空白字符。例如: ``` p = ' python\t' print(p.strip()) ``` 这样会删除字符串两侧的空白字符和制表符。 #### 引用[.reference_title] - *1* *3* [Python字符串变量基础](https://blog.csdn.net/weixin_51492608/article/details/127359878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python基础之字符串string类型)](https://blog.csdn.net/qq_34782203/article/details/127321588)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值