Python -zelfstudie

Python HOME Python-intro Python Aan de slag Python-syntaxis Python-opmerkingen Python-variabelen Python-gegevenstypen Python-nummers Python-casting Python-snaren Python Booleans Python-operators Python-lijsten Python-tupels Python-sets Python-woordenboeken Python Als...Anders Python While-lussen Python voor lussen Python-functies Python Lambda Python-arrays Python-klassen/objecten Python-overerving Python-iterators Python-bereik Python-modules Python-datums Python-wiskunde Python JSON Python RegEx Python PIP Python proberen...Behalve Python-gebruikersinvoer Opmaak van Python-tekenreeksen

Bestandsbehandeling

Python-bestandsafhandeling Python-bestanden lezen Python bestanden schrijven/maken Python bestanden verwijderen

Python-modules

NumPy-zelfstudie Panda walkthrough Scipy-zelfstudie

Python Matplotlib

Matplotlib Intro Matplotlib Aan de slag Matplotlib Pyplot Matplotlib plotten Matplotlib-markeringen Matplotlib-lijn Matplotlib-labels Matplotlib-raster Matplotlib-subplots Matplotlib Scatter Matplotlib-repen Matplotlib-histogrammen Matplotlib-cirkeldiagrammen

Machinaal leren

Beginnen Gemiddelde mediane modus Standaardafwijking percentiel Gegevensdistributie Normale gegevensverdeling Spreidingsplot Lineaire regressie Polynomiale regressie Meervoudige regressie Schaal Trein/Test Beslissingsboom

Python MySQL

MySQL Aan de slag MySQL Database maken MySQL-tabel maken MySQL-invoeging MySQL Select MySQL Waar MySQL Bestel op MySQL verwijderen MySQL-droptabel MySQL-update MySQL-limiet MySQL Join

Python MongoDB

MongoDB Aan de slag MongoDB Database maken MongoDB Verzameling maken MongoDB invoegen MongoDB Zoeken MongoDB-query MongoDB Sorteren MongoDB verwijderen MongoDB Drop-collectie MongoDB-update MongoDB-limiet

Python-referentie

Python-overzicht Ingebouwde functies van Python Python-stringmethoden Methoden voor Python-lijst Python-woordenboekmethoden Python Tuple-methoden Methoden voor Python-sets Python-bestandsmethoden Python-trefwoorden Python-uitzonderingen Python-woordenlijst

Modulereferentie

Willekeurige module Verzoekmodule Statistiekmodule Wiskundige module cMath-module

Python-instructies

Lijstduplicaten verwijderen Een string omkeren Voeg twee nummers toe

Python-voorbeelden

Python-voorbeelden Python-compiler Python-oefeningen Python-quiz Python-certificaat

Python String -codering() Methode

❮ Stringmethoden


Voorbeeld

UTF-8 codeert de string:

txt = "My name is Ståle"

x = txt.encode()

print(x)

Definitie en gebruik

De encode()methode codeert de tekenreeks met behulp van de opgegeven codering. Als er geen codering is opgegeven, wordt UTF-8 gebruikt.


Syntaxis

string.encode(encoding=encoding, errors=errors)

Parameterwaarden

Parameter Description
encoding Optional. A String specifying the encoding to use. Default is UTF-8
errors Optional. A String specifying the error method. Legal values are:
'backslashreplace' - uses a backslash instead of the character that could not be encoded
'ignore' - ignores the characters that cannot be encoded
'namereplace' - replaces the character with a text explaining the character
'strict' - Default, raises an error on failure
'replace' - replaces the character with a questionmark
'xmlcharrefreplace' - replaces the character with an xml character

Meer voorbeelden

Voorbeeld

Deze voorbeelden gebruiken ascii-codering en een teken dat niet kan worden gecodeerd, waarbij het resultaat met verschillende fouten wordt weergegeven:

txt = "My name is Ståle"

print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))

❮ Stringmethoden