write a program to create concatenate and print a string and accessing substring from a given string.

In this article you will learn a Python program that creates two strings  by asking a user to enter the string,  concat these two strings.  Finally take the First string and print all the substance from the given first thing.

let's try to perform these three  string operation in Python 

 

write a program to create, concatenate and print a string and accessing substring from a given string.

 

 


write a program to create, concatenate and print a string and accessing substring from a given string.

Let's take two variables named string1 and string2, and ask the user to enter the first and second string.

string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")





Using the print function call print the two two newly created strings.

print("Two Strings Created are ...")
print("String 1 : ", string1)
print("String 2 : ", string2)





Take a new variable named string3,  take the first and second string and concatenate it by using the plus operator.

string3 = string1 + " " +  string2
print("New String After Concatenating Two Strings is ...")
print("New String : ", string3)





Finally using the print function call print user friendly message and  using the for loop iterate all the substrings present in the string1  and print the substrings.

print("Accessing Substring from Given First String ...")
for substring in string1.split():
   print(substring)





Complete Python Program
===============================


string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")

print("Two Strings Created are ...")
print("String 1 : ", string1)
print("String 2 : ", string2)

string3 = string1 + " " +  string2
print("New String After Concatenating Two Strings is ...")
print("New String : ", string3)

print("Accessing Substring from Given First String ...")
for substring in string1.split():
   print(substring)



Output
================

Enter First String : hi how are you ?
Enter Second String : you there ?

Two Strings Created are ...
String 1 :  hi how are you ?
String 2 :  you there ?

New String After Concatenating Two Strings is ...
New String :  hi how are you ? you there ?

Accessing Substring from Given First String ...
hi
how
are
you
?





Output 2
==============

Enter First String : code with tj.
Enter Second String : learn programming

Two Strings Created are ...
String 1 :  code with tj.
String 2 :  learn programming

New String After Concatenating Two Strings is ...
New String :  code with tj. learn programming

Accessing Substring from Given First String ...
code
with
tj.




Another Version String Concatenate using Each Letter
========================================================

string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")

print("Two Strings Created are ...")
print("String 1 : ", string1)
print("String 2 : ", string2)

string3 = ""
for letter in string1:
   string3 += letter
string3 += " "
for letter in string2:
   string3 += letter
print("New String After Concatenating Two Strings is ...")
print("New String : ", string3)

print("Accessing Substring from Given First String ...")
for substring in string1.split():
  print(substring)



Conclusion
==================

The above Python program accepts two strings from the user,  concatenates the two strings and finally prints all the substring present in the first string.

Execute the above Python program by providing random input string as input to the script. Comment it down below if you have any queries regarding the above Python program.




Post a Comment

0 Comments