Hướng dẫn can you add an int and string in python? - bạn có thể thêm một int và chuỗi trong python không?

Giới thiệu

Python hỗ trợ kết hợp chuỗi bằng toán tử

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
2. Trong hầu hết các ngôn ngữ lập trình khác, nếu chúng ta nối một chuỗi với số nguyên (hoặc bất kỳ loại dữ liệu nguyên thủy nào khác), ngôn ngữ sẽ chăm sóc chúng thành một chuỗi và sau đó kết hợp nó.

Tuy nhiên, trong Python, nếu bạn cố gắng kết hợp một chuỗi với số nguyên bằng toán tử

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
2, bạn sẽ gặp lỗi thời gian chạy.

Thí dụ

Hãy cùng xem xét một ví dụ để kết hợp một chuỗi (

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
4) và số nguyên (
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
5) bằng toán tử
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
2.

string_concat_int.py

current_year_message = 'Year is '

current_year = 2018

print(current_year_message + current_year)

Đầu ra mong muốn là chuỗi:

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
7. Tuy nhiên, khi chúng tôi chạy mã này, chúng tôi sẽ gặp lỗi thời gian chạy sau:

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str

Vậy làm thế nào để bạn kết hợp

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
4 và
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
5 trong Python? Có nhiều cách khác để thực hiện hoạt động này.

Sử dụng chức năng print(current_year_message + str(current_year)) 0

Chúng ta có thể chuyển

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
5 cho hàm
print(current_year_message + str(current_year))
0, nó sẽ được chuyển đổi thành
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
4:

print(current_year_message + str(current_year))

Số nguyên

print(current_year_message + str(current_year))
4 được trả về dưới dạng chuỗi:
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
7.

Sử dụng toán tử nội suy print(current_year_message + str(current_year)) 6

Chúng ta có thể chuyển các giá trị sang đặc tả chuyển đổi với định dạng chuỗi kiểu printf:

print("%s%s" % (current_year_message, current_year))

Số nguyên

print(current_year_message + str(current_year))
4 được nội suy vào một chuỗi:
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
7.

Sử dụng chức năng print(current_year_message + str(current_year)) 9

Chúng ta cũng có thể sử dụng hàm

print(current_year_message + str(current_year))
9 để kết hợp chuỗi và số nguyên.

print("{}{}".format(current_year_message, current_year))

Số nguyên

print(current_year_message + str(current_year))
4 là loại bị ép buộc vào một chuỗi:
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
7.

Sử dụng dây F.

Nếu bạn đang sử dụng các phiên bản Python 3.6 trở lên, bạn cũng có thể sử dụng F-String.

print(f'{current_year_message}{current_year}')

Số nguyên

print(current_year_message + str(current_year))
4 được nội suy vào một chuỗi:
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
7.

Sử dụng chức năng print(current_year_message + str(current_year)) 9

Chúng ta cũng có thể sử dụng hàm

print(current_year_message + str(current_year))
9 để kết hợp chuỗi và số nguyên.

Trong Python, chúng ta thường thực hiện nối chuỗi bằng cách sử dụng toán tử

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
2. Tuy nhiên, toán tử
Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
2, như chúng ta đã biết, cũng được sử dụng để thêm số nguyên hoặc số dấu phẩy động.

Vậy điều gì sẽ xảy ra nếu chúng ta có một chuỗi và INT ở cả hai bên của toán hạng?

Vì Python là một ngôn ngữ được đánh máy động, chúng tôi sẽ không phải đối mặt với bất kỳ lỗi nào trong quá trình biên dịch, mà là, chúng tôi gặp lỗi thời gian chạy. (Cụ thể hơn, ngoại lệ kiểu loại được nâng lên)Runtime Error. (More specifically, a TypeError exception is raised)

Đoạn trích dưới đây chứng minh điều này:

a = "Hello, I am in grade "

b = 12

print(a + b)

Đầu ra

Traceback (most recent call last):
  File "concat.py", line 5, in 
    print(a + b)
TypeError: can only concatenate str (not "int") to str

Vì vậy, vì chúng ta không thể trực tiếp kết hợp một số nguyên với một chuỗi, chúng ta cần điều khiển các toán hạng để chúng có thể được nối. Có nhiều cách để làm điều này.


1. Sử dụng str ()

Chúng ta có thể chuyển đổi số nguyên thành một chuỗi, thông qua hàm

print(current_year_message + str(current_year))
0. Bây giờ, chuỗi mới bây giờ có thể được nối với chuỗi khác để cung cấp đầu ra;

Đầu ra

Vì vậy, vì chúng ta không thể trực tiếp kết hợp một số nguyên với một chuỗi, chúng ta cần điều khiển các toán hạng để chúng có thể được nối. Có nhiều cách để làm điều này.

1. Sử dụng str ()

Chúng ta có thể chuyển đổi số nguyên thành một chuỗi, thông qua hàm print(current_year_message + str(current_year)) 0. Bây giờ, chuỗi mới bây giờ có thể được nối với chuỗi khác để cung cấp đầu ra;

a = "Hello, I am in grade "

b = 12

print("{}{}".format(a, b))

Đây là cách phổ biến nhất để chuyển đổi một số nguyên thành một chuỗi.

Nhưng, chúng ta cũng có thể sử dụng các phương pháp khác.

a = "Hello, I am in grade "

b = 12

print("%s%s" % (a, b))

2. Sử dụng định dạng ()

Đầu ra vẫn giống như trước đây.

3. Sử dụng trình xác định định dạng ’%

Mặc dù chúng tôi có thể chỉ định rằng cả hai chuỗi

print("%s%s" % (current_year_message, current_year))
8 và
print("%s%s" % (current_year_message, current_year))
9, chúng tôi cũng có thể sử dụng các định dạng định dạng kiểu C (
print("{}{}".format(current_year_message, current_year))
0,
print("{}{}".format(current_year_message, current_year))
1) để kết hợp một số nguyên với một chuỗi.

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
0

Đầu ra vẫn giữ nguyên cho mã trên.

4. Sử dụng dây F.

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
1

Chúng ta có thể sử dụng các chuỗi Python F trên Python 3.6 trở lên để kết hợp một số nguyên với một chuỗi.


Sự kết luận

Trong bài viết này, chúng tôi đã học cách kết hợp một số nguyên vào một chuỗi bằng các phương pháp khác nhau.

Người giới thiệu

  • Stackoverflow Câu hỏi về nối dây của chuỗi và int
  • Bài viết của tạp chí về nối dây của chuỗi và int

Bạn có thể thêm chuỗi và int không?

Để kết hợp một chuỗi vào giá trị INT, hãy sử dụng toán tử nối.Đây là INT của chúng tôi.int val = 3;Bây giờ, để kết hợp một chuỗi, bạn cần khai báo một chuỗi và sử dụng toán tử +.. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.

Làm thế nào để bạn thêm số vào chuỗi và số nguyên trong Python?

Trong Python, chúng ta thường thực hiện kết nối chuỗi bằng toán tử +.Tuy nhiên, toán tử + như chúng ta biết, cũng được sử dụng để thêm số nguyên hoặc số dấu nổi.using the + operator. The + operator, however as we know, is also used to add integers or floating-point numbers.

Bạn có thể thêm một số vào một chuỗi trong Python không?

Python Thêm chuỗi với toán tử + cách dễ nhất để nối các chuỗi là sử dụng toán tử + hoặc + =.Toán tử + được sử dụng cả để thêm số và chuỗi;Trong lập trình, chúng tôi nói rằng toán tử bị quá tải.use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.

Điều gì xảy ra khi bạn thêm một chuỗi và một số nguyên?

Chỉ cần thêm vào int hoặc số nguyên một chuỗi trống "" và bạn sẽ nhận được int của mình dưới dạng chuỗi.Nó xảy ra vì thêm int và chuỗi cung cấp cho bạn một chuỗi mới.Điều đó có nghĩa là nếu bạn có int x = 5, chỉ cần xác định x + "" và bạn sẽ nhận được chuỗi mới của mình.gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.