python 字符串运算_字符串运算符以及python中的示例

python 字符串运算

Python中的字符串运算符 (String operators in Python)

There are basically three operators which can be used with the string python:

基本上可以将三个运算符与python字符串一起使用:

  1. Python + Operator

    Python +运算子

  2. Python * Operator

    Python *运算子

  3. Python in Operator

    Python在运营商

1)Python“ +”运算符 (1) Python "+" Operator )

"+" Operator is known as concatenating operator in Python, it is used to concatenate/join two or more strings.

“ +”运算 符在Python中被称为串联运算符 ,它用于串联/连接两个或多个字符串。

Example 1: Concatenating string variables

示例1:串联字符串变量

# python code to demonstrate an example
# of string "+" operator 

str1 = "Hello"
str2 = " "
str3 = "world!"

result = str1+str2+str3

print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)
print("result: ", result)

Output

输出量

str1:  Hello
str2: 
str3:  world!
result:  Hello world!

Example 2: Adding strings, string variables within the print() function

示例2:在print()函数中添加字符串,字符串变量

# python code to demonstrate an example
# of string "+" operator 

name = "prem"
print("My name is " + name)

print("Hello" + " " + "world!")

Output

输出量

My name is prem
Hello world!

2)Python“ *”运算符 (2) Python "*" Operator)

" * " Operator is used to create multiple copies of a string.

“ *”运算符用于创建一个字符串的多个副本。

Example 1:

范例1:

# python code to demonstrate an example
# of string "*" operator 

result1 = "Hello"*3
print("result1: " + result1)

strval = "Hello world "
n = 5

result2 = strval*n
print("result2: " + result2)

Output

输出量

result1: HelloHelloHello
result2: Hello world Hello world Hello world Hello world Hello world

Example 2: Testing with zero or negative value

示例2:使用零或负值进行测试

If we multiply a string with either 0 (zero) or a negative number, the result will be an empty string.

如果我们将一个字符串与0(零)或负数相乘,结果将是一个空字符串。

# python code to demonstrate an example
# of string "*" operator 

result1 = "Hello"* 0
print("result1: " + result1)

strval = "Hello world "
n = -5

result2 = strval*n
print("result2: " + result2)

Output

输出量

result1:
result2:

3)Python“中”运算符 (3) Python "in" Operator)

"in operator" is used to check whether a given value (first operand) exists in the string (second operand) or not. It returns True if the first operand exists in the second operand, else it returns False.

“ in运算符”用于检查字符串(第二个操作数)中是否存在给定值(第一个操作数)。 如果在第二个操作数存在的第一个操作数返回true,否则返回FALSE。

Example:

例:

# python code to demonstrate an example
# of string "in" operator 

str1 = "IncludeHelp.com is very popular"

str2 = "is"
if str2 in str1:
    print("\'" + str2 +  "\'" + " exists in " + str1)
else:
    print("\'" + str2 +  "\'" + " does not exist in " + str1)

str2 = "are"
if str2 in str1:
    print("\'" + str2 +  "\'" + " exists in " + str1)
else:
    print("\'" + str2 +  "\'" + " does not exist in " + str1)    

Output

输出量

'is' exists in IncludeHelp.com is very popular
'are' does not exist in IncludeHelp.com is very popular


翻译自: https://www.includehelp.com/python/string-operators-with-examples.aspx

python 字符串运算

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值