Home » Python » Python programs

Python program to print a string, extract characters from the string

Printing string, extracting characters from the string in Python: Here, we will learn how to print a complete string, how to access characters, characters from specific indexes from the string in python?
Submitted by IncludeHelp, on April 09, 2019

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 code to print string characters

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

# 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.