FAQ: Introduction to Strings - Concatenating Strings

This community-built FAQ covers the “Concatenating Strings” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Concatenating Strings

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello!

Could someone explain why in this excercise, this code does not work as the solution?

def account_generator(first_name, last_name):
  new_account = first_name[:3] + last_name[:3]
  return new_account

According to this exercise solution, I first have to create a variable within the function called “account_name” and then outside the function equate “new_account” to the function in order to get the correct answer.

Hello :slight_smile:

Well, your function is correct and it should be accepted as the solution of the first step of instructions.

But in order to finish this exercise, you have to create a new variable new_account and assign to it the result of calling your function with arguments "Julie" (first_name) and "Blevins" (last_name).

If the instruction is not clear feel free to use Get Code Solution option that you will find under Get Help button (right bottom corner). If the solution code does not make sense - get back to us, we will try to help :slight_smile:

4 Likes

Hi, one thing that I haven’t seen explained yet is:

Concatenating strings with plus “+” versus comma “,”

Example from exercise:

fruit_sentence = "My favorite fruit is" + favorite_fruit
 
print(fruit_sentence)
# Output: My favorite fruit isblueberries
 
fruit_sentence = "My favorite fruit is " + favorite_fruit
 
print(fruit_sentence)
# Output: My favorite fruit is blueberries

When using “+” you have to manually add spaces and convert integers to strings.
When using “,” you do not need to convert to strings or apply spaces.

Can anybody give an example of when you would utilise one over the other?
At this stage I see using a comma “,” as a faster and more accurate option of the two.

To break the myth, concatenating with a comma is not a thing. The only place a comma can be used is in the argument for, console.log(). it looks like concatenation but is actually just a space character between items in a comma separated list, sans comma.

1 Like

Hello, something that I’m still not quite understanding is that new_account coming back as not defined.

first_name = "Julie"
last_name = "Blevins"

def account_generator(first_name, last_name):
  account_name = first_name[:3] + last_name[:3]
  return account_name
new_account = account_name
print(new_account)

Why is this the case? If account_name contains the function of first_name[:3] + last_name[:3] then why do I need to add in
new_account = account_generator(first_name, last_name) instead of new_account = account_name?

If the temporary variable is holding what is to the right of it and code is read from a top down approach wouldnt that mean that anything above new_account = account_name would be applied? Isnt that why we can have variables not in the function that are able to be accessed anywhere in the code so long as they are out of the function?

What am I missing in my understanding?

The last bit of code needs to be:

new_account = account_generator("Julie", "Blevins")

1 Like

I am having trouble understanding the point of “return”.
Following the syntax learned, I am finding the correct solutions, but I also find additional solutions with the same results.

Why does one function work better than the other? Why isn’t the use of “return” considered too repetitive?

first_name = "Julie" last_name = "Blevins" def account_generator(first_name, last_name): new_account = first_name[:3] + last_name[:3] return new_account new_account = account_generator(first_name, last_name) print(new_account) def account_generator(): print(first_name[:3] + last_name[:3]) account_generator()

I can’t see the lesson that this FAQ is attached to b/c I didn’t do the CS path. I’m assuming that it’s asking you to write a function that takes some input and returns something, yea?

That said, return exits the function and returns a value to the caller.

print() is not the same thing as return. print() is a function that will print a string to the console.

See:

and:

1 Like

new_account = account_generator(first_name, last_name)
will also work

In the code in the exercise, it feels a bit confusing to have first_name and last_name globally defined, and also have them as the names for the local variables within the account_generator function. Would it not be clearer to have them with separate names?

first_name = "Julie" last_name = "Blevins" def account_generator(first_name,last_name): return first_name[:3]+last_name[:3] new_account = account_generator(first_name,last_name)