如何在python3.6中编码一个字符串..?

时间:2018-01-15 05:39:13

标签: python

我的代码是

import base64
var = "this is a program for string capitalize method"
var1 = base64.b64encode(bytes('var','utf-8'))
print("String Encode:",end=''),print(var1.encode('base64','strict'))

但是我收到以下错误:

  

AttributeError:'bytes'对象没有属性'encode'

1 个答案:

答案 0 :(得分:0)

第3行替换' var'与var。
第4行直接打印var1。

import base64
var = "this is a program for string capitalize method"
var1 = base64.b64encode(bytes(var,'utf-8')) # to encode above text
print("String Encode:",end=''),print(var1)) # var1 encoded string in bytes