Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Merge Strings | Strings
Tipos de Dados em Python

book
Merge Strings

In programming, there are no insurmountable obstacles; persistent practice can overcome any challenge. Therefore, let us discuss another essential aspect related to strings.

It is valuable to understand how to merge strings. In Python, this is achieved effortlessly by using the + operator. While this may seem obvious, I will provide a detailed explanation as customary. Consider the following example (a well-known string from The Beatles' song 'Tell Me Why').

string1 = "Tell me why you cried,"
string2 = " and why you lied to me"
string = string1 + string2

print(string)
12345
string1 = "Tell me why you cried," string2 = " and why you lied to me" string = string1 + string2 print(string)
copy

Note

This process is referred to as "concatenation," and it functions much like glue, essentially combining them together. If we desire strings to appear more legible, it is advisable to include spaces since concatenation does not automatically insert them.

Tarefa

Swipe to start coding

One more task for you:

  1. Create a variable output1 that combines string1 and string2 to form the phrase "country: Italy".
  2. Create a variable output2 that combines string1 and string3 to form the phrase "country: Ukraine".
  3. Create a variable output3 that combines string1 and string4 to form the phrase "country: Egypt".

Solução

string1 = "country: "
string2 = "Italy"
string3 = "Ukraine"
string4 = "Egypt"

# Merge variables to form phrases
output1 = string1 + string2
output2 = string1 + string3
output3 = string1 + string4

print("Variable output1 equals:", f'"{output1}"')
print("Variable output2 equals:", f'"{output2}"')
print("Variable output3 equals:", f'"{output3}"')

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 13
string1 = "country: "
string2 = "Italy"
string3 = "Ukraine"
string4 = "Egypt"

# Merge variables to form phrases
output1 = string1 + ___
output2 = ___ + ___
output3 = ___

print("Variable output1 equals:", f'"{output1}"')
print("Variable output2 equals:", f'"{output2}"')
print("Variable output3 equals:", f'"{output3}"')

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt