forked from WebJournal/journaldev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_concatenation.py
More file actions
72 lines (47 loc) · 1.38 KB
/
string_concatenation.py
File metadata and controls
72 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# concatenating multiple strings
s1 = 'Apple'
s2 = 'Pie'
s3 = 'Sauce'
s4 = s1 + s2 + s3
print(s4)
# string concatenation from user input
s1 = input('Please enter the first string:\n')
s2 = input('Please enter the second string:\n')
print('Concatenated String =', s1 + s2)
# using join() method
s1 = 'Hello'
s2 = 'World'
print('Concatenated String using join() =', "".join([s1, s2]))
print('Concatenated String using join() and whitespaces =', " ".join([s1, s2]))
# string concatenation with other objects
print('Hello' + str(4))
class Data:
id = 0
def __init__(self, i):
self.id = i
def __str__(self):
return 'Data[' + str(self.id) + ']'
print('Hello ' + str(Data(10)))
# using % operator
s1 = 'Hello'
s2 = 'World'
s3 = "%s %s" % (s1, s2)
print('String Concatenation using % Operator =', s3)
s3 = "%s %s from JournalDev - %d" % (s1, s2, 2018)
print('String Concatenation using % Operator with Formatting =', s3)
# using format() function
s1 = 'Hello'
s2 = 'World'
s3 = "{}-{}".format(s1, s2)
print('String Concatenation using format() =', s3)
s3 = "{in1} {in2}".format(in1=s1, in2=s2)
print('String Concatenation using format() =', s3)
# using f-string, PEP 498 Literal String Interpolation
s1 = 'Hello'
s2 = 'World'
s3 = f'{s1} {s2}'
print('String Concatenation using f-string =', s3)
name = 'Pankaj'
age = 34
d = Data(10)
print(f'{name} age is {age} and d={d}')