python字符串_Python字符串

python字符串

Good day, learners. In this tutorial we are going to learn Python String. In our previous tutorial we learned about Python Tuple.

美好的一天,学习者。 在本教程中,我们将学习Python String。 在上一教程中,我们了解了Python Tuple

Python字符串 (Python String)

One of the most common data type of python is String. “str” is the built in string class of python. String literals can be enclosed by single or double quotes. You can see some example of string literals below

python是String最常见的数据类型之一。 “ str”是python的内置字符串类。 字符串文字可以用单引号或双引号引起来。 您可以在下面看到一些字符串文字的示例

literal1 = "This is the first literal"
literal2 = "This is the second literal"

访问Python字符串 (Accessing the Python String)

You can print the whole string or a specific portion of the string which is called substring. To do so, you need to know some basics. The Python Strings are indexed from zero. That means if the size of the string is 5, the index of the elements are 0 to 4. The following code will help you to understand the context

您可以打印整个字符串或字符串的特定部分,称为子字符串。 为此,您需要了解一些基础知识。 Python字符串从零开始索引。 这意味着如果字符串的大小为5,则元素的索引为0到4。下面的代码将帮助您理解上下文。

word = "Tobacco"
#index: 0123456

#This will print the whole word
print(word)    	#Tobacco

#This will print the size of the string
print(len(word))	#7

#This will print the 0th element of the string which is T
print(word[0])	#T

#prints the 1st element (o) to 4th element (c) of the string
print(word[1:5])	#obac

#prints the substring from 3rd element to the end of the string
print(word[3:])	#acco

#prints from the 0th element to 2nd element of the string
print(word[:3])	#Tob

The output of the following code will be

以下代码的输出将是

================== RESTART: /home/imtiaz/str.py ==================
Tobacco
7
T
obac
acco
Tob

串联python字符串 (Concatenating the python string)

You can concat two string by simply placing a “+” operator between them. You can concat a number with the string, but the condition is you have to change the number to a string. You can use str() function to convert a number to string. The following example will give you some idea about this

您可以通过在两个字符串之间放置一个“ +”运算符来连接它们。 您可以使用字符串连接数字,但条件是必须将数字更改为字符串。 您可以使用str()函数将数字转换为字符串。 以下示例将使您对此有所了解

str1 = "I love"
str2 = "I hate"
str3 = " you!"
#example of concatenation between two string
print(str1 + str3)

#this will give an error
#print("My age is "+15)

#after converting the number to a string, concatenate it with a string
print("My age is "+str(15))

The output of the following code will be

以下代码的输出将是

更新Python字符串 (Updating Python String)

Python string does not allow to update element of the string. However, you can try slicing technique, to create a new string withe updated specific index of the string. Suppose, we have a word “toek”, but we want to make it “took”. Now, look at the word, the element “e” that needs to be updated is at index 2. So we can slice the substrings before and after “e”, those are “to” and “k” respectively. Then we can concatenate “to” with the updated element “o” and after that we can concatenate the resultant string with “k”. So the code will illustrate the idea

Python字符串不允许更新字符串的元素。 但是,您可以尝试使用切片技术来创建具有更新的字符串特定索引的新字符串。 假设我们有一个单词“ toek”,但我们想使其成为“ took”。 现在,看一下单词,需要更新的元素“ e”位于索引2。因此,我们可以对“ e”之前和之后的子字符串进行切片,分别是“ to”和“ k”。 然后,我们可以将“ to”与更新后的元素“ o”连接起来,然后再将结果字符串与“ k”连接起来。 所以代码将说明这个想法

str1 = 'toek'
print("Before Update:")
print(str1)

first = str1[:2] #that is 'to'
update = 'o'
last = str1[3:] #that is 'k'

str1 = first + update + last

print("After Update:")
print(str1)

The output will be

输出将是

================== RESTART: /home/imtiaz/str3.py ==================
Before Update:
toek
After Update:
took
>>>

Python字符串方法 (Python String Methods)

There are some methods to manipulate Python String. You can find all the python string method to their official here. Most common python string methods are shown below:

有一些方法可以操纵Python String。 您可以在此处找到所有官方的python字符串方法。 最常见的python字符串方法如下所示:

  • lower(): returns the lowercase version of the string

    lower():返回字符串的小写版本
  • upper(): returns the uppercase version of the string

    upper():返回字符串的大写版本
  • strip(): returns a string with whitespace removed from the start and end

    strip():返回从开头和结尾删除空格的字符串
  • isalnum(): return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.

    isalnum():如果字符串中的所有字符都是字母数字并且至少有一个字符,则返回true,否则返回false。
  • isalpha(): return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

    isalpha():如果字符串中的所有字符都是字母并且至少包含一个字符,则返回true,否则返回false。
  • title(): return a title-cased version of the string where words start with an uppercase character and the remaining characters are lowercase.

    title():返回字符串的标题大小写版本,其中单词以大写字母开头,其余字符为小写字母。
  • join(list): joins the elements in the given list together using the string as the delimiter

    join(list):使用字符串作为分隔符将给定列表中的元素连接在一起
  • find(substring): returns the lowest index in the string where substring is found. If substring is not found, it returns -1.

    find(substring):返回找到子字符串的字符串中的最低索引。 如果未找到子字符串,则返回-1。

使用Python字符串的转义序列 (Escape Sequence with Python String)

You can put escape sequence in string literal to perform some special task. Suppose, you have two words “cat” and “dog”. You want to put them in one string literal but want then in separate line. To do so, you can add ‘\n’ between these two words. The following example will help you to understand.

您可以将转义序列放在字符串文字中以执行某些特殊任务。 假设您有两个单词“ cat”和“ dog”。 您想将它们放在一个字符串文字中,然后再放在单独的行中。 为此,您可以在这两个词之间添加“ \ n”。 以下示例将帮助您理解。

task = 'cat\ndog'

print(task)

The output will print ‘cat’ and ‘dog’ in separate line. There are some escape sequences. If you’re interested, you can find it here

输出将在单独的行中打印“ cat”和“ dog”。 有一些转义序列。 如果您有兴趣,可以在这里找到

Python字符串包含 (Python String contains)

If you want to check if a substring is present in a string or not, then we can use in operator as shown in below example.

如果要检查字符串中是否存在子字符串,则可以使用in运算符,如下面的示例所示。

str1 = "I am here"

if "I" in str1:
    print("Found")
else:
    print("Not Found")

Python字符串拆分 (Python String split)

Sometimes we get a long string with delimiter and we want to split them into a list. For example it’s most common in CSV data. We can use string split function for this.

有时我们用定界符得到一个长字符串,我们想将它们分成一个列表。 例如,在CSV数据中最常见。 我们可以为此使用字符串拆分功能。

x = "1,2,3"

y = x.split(",")

print(y)

It will print below output.

它将在输出下方打印。

>>> 
================= RESTART: /Users/pankaj/Desktop/string1.py =================
['1', '2', '3']
>>>

So, this is all about basic Python String. Hope that you understood well. If you have any query regarding Python String, feel free to ask in the comment section.

因此,这一切都与基本的Python String有关。 希望你能理解。 如果您对Python String有任何疑问,请随时在评论部分提问。

翻译自: https://www.journaldev.com/14385/python-string

python字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值