How to read two strings in python

  • Python Code to Read two Strings & Concatenate the Strings.
    • Python code implementation without user-defined functions & classes
    • Python code implementation using function
    • Python code implementation using Classes

 </p> <h3><span class="ez-toc-section" id="Python_code_implementation_without_user-defined_functions_classes"></span><strong><span style="color:#ff0000">Python code implementation without user-defined functions &amp; classes</span></strong><span class="ez-toc-section-end"></span></h3> <h5><span style="color:#339966"><strong>Code:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="atomic"># Python code to Read two Strings &amp; Concatenate the Strings a = input["Enter First String: "] b = input["Enter Second String: "] c = a+b print["Concatenated String is: ",c] </pre> <h5></h5> <h5><span style="color:#339966"><strong>Executing Python Script:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">python program.py Enter First String: I love Enter Second String: Python Programming </pre> <p><span id="ezoic-pub-ad-placeholder-125" class="ezoic-adpicker-ad"></span><span class="ezoic-ad ezoic-at-0 medrectangle-3 medrectangle-3125 adtester-container adtester-container-125 ezoic-ad-adaptive" data-ez-name="pythonbaba_com-medrectangle-3"><span class="ezoic-ad medrectangle-3 medrectangle-3-multi-125 adtester-container adtester-container-125" data-ez-name="pythonbaba_com-medrectangle-3"><span id="div-gpt-ad-pythonbaba_com-medrectangle-3-0" ezaw="290" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:290px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none">if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[250,250],'pythonbaba_com-medrectangle-3','ezslot_3',125,'0','0']]};__ez_fad_position['div-gpt-ad-pythonbaba_com-medrectangle-3-0'];if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[250,250],'pythonbaba_com-medrectangle-3','ezslot_4',125,'0','1']]};__ez_fad_position['div-gpt-ad-pythonbaba_com-medrectangle-3-0_1']; </p> <h5><span style="color:#339966"><strong>Output:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">Concatenated String is: I lovePython Programming </pre> <p>&nbsp;</p> <p>&nbsp;</p> <h3><span class="ez-toc-section" id="Python_code_implementation_using_function"></span><strong><span style="color:#ff0000">Python code implementation using function</span></strong><span class="ez-toc-section-end"></span></h3> <h5><span style="color:#339966"><strong>Code:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="atomic"># Python code to Read two Strings &amp; Concatenate the Strings using Function def concatenate_string_function[a_string, b_string]: return a_string + b_string a = input["Enter First String: "] b = input["Enter Second String: "] c = concatenate_string_function[a, b] print["Concatenated String is: ",c] </pre> <p>&nbsp;</p> <h5><span style="color:#339966"><strong>Executing Python Script:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">python program.py Enter First String: I love Enter Second String: Python Programming </pre> <p>&nbsp;</p> <h5><span style="color:#339966"><strong>Output:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">Concatenated String is: I lovePython Programming </pre> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <h3><span id="ezoic-pub-ad-placeholder-127" class="ezoic-adpicker-ad"></span><span class="ezoic-ad ezoic-at-0 large-leaderboard-2 large-leaderboard-2127 adtester-container adtester-container-127" data-ez-name="pythonbaba_com-large-leaderboard-2"><span id="div-gpt-ad-pythonbaba_com-large-leaderboard-2-0" ezaw="250" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;width:100%;max-width:1200px;margin-left:auto!important;margin-right:auto!important;min-height:250px;min-width:970px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none">if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[250,250],'pythonbaba_com-large-leaderboard-2','ezslot_5',127,'0','0']]};__ez_fad_position['div-gpt-ad-pythonbaba_com-large-leaderboard-2-0'];</script></span></span><span class="ez-toc-section" id="Python_code_implementation_using_Classes"></span><strong><span style="color:#ff0000">Python code implementation using</span></strong><strong><span style="color:#ff0000"> Classes</span></strong><span class="ez-toc-section-end"></span></h3> <h5><span style="color:#339966"><strong>Code:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="atomic"># Python code to Read two Strings &amp; Concatenate the Strings using Class class Concatenate_string_class[object]: def concatenate_string_function[self, a_string, b_string]: self.a_string = a_string self.b_string = b_string return self.a_string + self.b_string a = input["Enter First String: "] b = input["Enter Second String: "] #Create the Object Object_1 = Concatenate_string_class[] #Call the function using created Object c = Object_1.concatenate_string_function[a, b] print["Concatenated String is: ",c] </pre> <h5></h5> <h5><span style="color:#339966"><strong>Executing Python Script:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">python program.py Enter First String: I love Enter Second String: Python Programming </pre> <p>&nbsp;</p> <h5><span style="color:#339966"><strong>Output:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">Concatenated String is: I lovePython Programming </pre> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p><strong>Enjoy Python Code By Pythonbaba 🙂</strong></p> <p>&nbsp;</p> <div class="fb-comments" data-href="//developers.facebook.com/docs/plugins/comments#configurator" data-width="" data-numposts="5"></div> <div class="saboxplugin-wrap" itemtype="//schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img srx="//pythonbaba.com/wp-content/uploads/2019/10/Pythonbaba-logo.png" width="100" height="100" alt="Learn Python Programming at PythonBaba.com" itemprop="image" src="//pythonbaba.com/wp-content/uploads/2019/10/Pythonbaba-logo.png" class="lazyload"><noscript><img srx="//pythonbaba.com/wp-content/uploads/2019/10/Pythonbaba-logo.png" width="100" height="100" alt="Learn Python Programming at PythonBaba.com" itemprop="image"></noscript></div><div class="saboxplugin-authorname"><a target="_blank" href="//pythonbaba.com/author/pythonbaba/" class="vcard author" rel="author" itemprop="url"><span class="fn" itemprop="name">PythonBaba</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Our team loves to write in Python, Linux, Bash, HTML, CSS Grid, CSS Flex, and Javascript. We love to write technical articles.</p> <p>Currently exploring Data Science, Machine learning and Artificial intelligence.</p> </div></div><div class="clearfix"></div></div></div> <div class="td-g-rec td-g-rec-id-content_bottom tdi_5 td_block_template_1"> <style> /* custom css */ .tdi_5.td-a-rec{ text-align: center; }.tdi_5 .td-element-style{ z-index: -1; }.tdi_5.td-a-rec-img{ text-align: left; }.tdi_5.td-a-rec-img img{ margin: 0 auto 0 0; } @media [max-width: 767px] { .tdi_5.td-a-rec-img { text-align: center; } }

if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[250,250],'pythonbaba_com-large-mobile-banner-1','ezslot_6',126,'0','0']]};__ez_fad_position['div-gpt-ad-pythonbaba_com-large-mobile-banner-1-0'];
Facebook
Twitter
Pinterest
WhatsApp
    Previous articlePython code to Check if a given String is Palindrome
    Next articlePython code that combines two lists by taking elements alternately
    PythonBaba
    Our team loves to write in Python, Linux, Bash, HTML, CSS Grid, CSS Flex, and Javascript. We love to write technical articles. Currently exploring Data Science, Machine learning and Artificial intelligence.
    /* custom css */ .tdi_6.td-a-rec{ text-align: center; }.tdi_6 .td-element-style{ z-index: -1; }.tdi_6.td-a-rec-img{ text-align: left; }.tdi_6.td-a-rec-img img{ margin: 0 auto 0 0; } @media [max-width: 767px] { .tdi_6.td-a-rec-img { text-align: center; } }

    How do you get two strings in Python?

    Using '+' operator Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.

    How do you read two strings on the same line in Python?

    You'd have to read byte-by-byte and look for the space character, which is very inefficient. Better to read the entire line, and then split the resulting string on the space, giving you two strings. May be one line of code, but it's still doing what I described: read a line, and split it.

    How do you read a string in Python?

    To read a string from console as input to your Python program, you can use input[] function. input[] can take an argument to print a message to the console, so that you can prompt the user and let him/her know what you are expecting.

    Chủ Đề