Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Concatenation | Common data types
Learn Python from Scratch

book
Concatenation

There are also some interesting operations available for strings in Python. One of them is concatenating strings, which means gluing one string to another. In Python, it can be easily done with + sign (ensure that both sides must be either both strings or numbers)

Also, you can easily calculate the number of each symbol met in your row. Use s.count(x) to do it where s - is a variable where string stores and x - symbol you are interested in.

Also, you can replicate one string multiple times by just using multiplication. string * n will produce n copies of string.

For example,

# let's concatenate strings
print("I " + "can " + "concatenate " + "strings")
# let's count number of a in specific string
s = "Linear discriminant analysis"
print(s.count("a"))
# replicate string multiple times
print("test " * 5)
1234567
# let's concatenate strings print("I " + "can " + "concatenate " + "strings") # let's count number of a in specific string s = "Linear discriminant analysis" print(s.count("a")) # replicate string multiple times print("test " * 5)
copy
Tâche

Swipe to start coding

Given two variables: name and surname with values "Alex" and "Ferguson" respectively.

  1. You need to create variable fullname and assign the result of the concatenation of name and surname. Do not forget about the blank space between name and surname (use + " ")! Then print this fullname.
  2. Count number of letter 'e' is fullname.

Solution

# variables name and surname
name = "Alex"
surname = "Ferguson"
# create variable fullname
fullname = name + " " + surname
# print fullname
print("Full name:", fullname)
# count number of letter i in fullname
print("Letter e is met in fullname:", fullname.count('e'), "times")

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 6
# variables name and surname
name = "Alex"
surname = "Ferguson"
# create variable fullname
fullname = _ _ _ + _ _ _ + _ _ _
# print fullname
print("Full name:", _ _ _)
# count number of letter i in fullname
print("Letter e is met in fullname:", _ _ _._ _ _(_ _ _), "times")
toggle bottom row
some-alt