Python程序打印字符串,从字符串中提取字符

In this program – we are going to learn how can we complete string, print specific characters, print a range of the characters, print string multiple times (using * operator), print multiple stings by concatenating them etc.

在此程序中,我们将学习如何完成字符串 ,打印特定字符,打印字符范围,多次打印字符串(使用*运算符),通过将字符串串联来打印多个字符串等。

Syntax to print string in different ways:

以不同方式打印字符串的语法:

    print (string)          	# printing complete string
    print (string[i])       	# printing ith element of string
    print (string[i], string[j])# printing ith and jth elements
    print (string[i:j])     	# printing elements from ith index to jth index
    print (string[i:])      	# printing all elements from ith index
    print (string * 2)  	# printing string two times
    print (string1 + string2) 	# printing concatenated string1 & string2

Python代码打印字符串字符 (Python code to print string characters)

Here, we have two strings str1 and str2 and we are printing elements in the different ways.

在这里,我们有两个字符串str1和str2,并且以不同的方式打印元素。

# python program to demonstrate example of string

# declaring & initializing two strings
str1 = "IncludeHelp"
str2 = ".com"

print (str1)          		# printing complete str1
print (str1[0])       		# printing 0th (first) elements of str1
print (str1[0], str1[1])	# printing first & second elements
print (str1[2:5])     		# printing elements from 2nd to 5th index
print (str1[1:])      		# printing all elements from 1st index
print (str2 * 2)  		# printing str2 two times
print (str1 + str2) 		# printing concatenated str1 & str2

Output

输出量

IncludeHelp
I
I n
clu
ncludeHelp
.com.com
IncludeHelp.com


翻译自: https://www.includehelp.com/python/print-a-string-extract-characters-from-the-string.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值