Saturday, April 14, 2018

Sample program – Python Strings

Posted by AnithaKamatchi on Saturday, April 14, 2018 in | No comments


pythonStrings.py
#Python strings
string_1 = 'Welcome to python strings' #strings assigned within single quotes
string_2 = "Python strings introduction" #strings assigned within double quotes
print ("String_1: ",string_1,"\nString_2: ",string_2)
print ("String from a given index: " , string_1[0])
print("Substring from given range: ",string_1[0:7])
print ("String Concatenation: ",string_1+"."+  string_2)
print ("String repetition: ",string_1[0] * 3)
print("Check the occurrence of a character in a string: ",'W' in string_1)
print("Check the non-occurrence of a character in a string: ",'W' not in string_1)
print("\nParagraph with triple single/double quotes: " , '''In python \t
multiple line of strings can be represented within triple quotes
''')

output:
String_1:  Welcome to python strings
String_2:  Python strings introduction
String from a given index:  W
Substring from given range:  Welcome
String Concatenation:  Welcome to python strings.Python strings introduction
String repetition:  WWW
Check the occurrence of a character in a string:  True
Check the non-occurrence of a character in a string:  False

Paragraph with triple single/double quotes: In python  
multiple line of strings can be represented within triple quotes



0 comments:

Post a Comment