<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly]]></title><description><![CDATA[<p dir="auto">What does exactly mean this error? I'm confused</p>
]]></description><link>https://forum.pycom.io/topic/1192/typeerror-can-t-convert-str-object-to-str-implicitly</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 16:47:29 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/1192.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 May 2017 15:24:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Thu, 11 May 2017 15:24:28 GMT]]></title><description><![CDATA[<p dir="auto">What does exactly mean this error? I'm confused</p>
]]></description><link>https://forum.pycom.io/post/7347</link><guid isPermaLink="true">https://forum.pycom.io/post/7347</guid><dc:creator><![CDATA[Andrea Filippini]]></dc:creator><pubDate>Thu, 11 May 2017 15:24:28 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Thu, 11 May 2017 16:41:17 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1230">@Andrea-Filippini</a>,</p>
<p dir="auto">Could you please share the code that caused this error?</p>
<p dir="auto">Thanks,</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.pycom.io/post/7357</link><guid isPermaLink="true">https://forum.pycom.io/post/7357</guid><dc:creator><![CDATA[bucknall]]></dc:creator><pubDate>Thu, 11 May 2017 16:41:17 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Fri, 19 May 2017 13:23:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/823">@bucknall</a> My question is not really relative to a piece of code in particular, but here's an example:</p>
<pre><code>s = b'foo|bar'
sp = s.split('|')
</code></pre>
<p dir="auto">while this</p>
<pre><code>s = b'foo|bar'
sp = str(s).split('|')
</code></pre>
<p dir="auto">and this works</p>
<pre><code>s = b'foo|bar'
sp = s.decode('utf-8').split('|')
</code></pre>
<p dir="auto">Please explain the difference</p>
]]></description><link>https://forum.pycom.io/post/7607</link><guid isPermaLink="true">https://forum.pycom.io/post/7607</guid><dc:creator><![CDATA[Andrea Filippini]]></dc:creator><pubDate>Fri, 19 May 2017 13:23:14 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Sat, 20 May 2017 15:28:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1230">@Andrea-Filippini</a> said in <a href="/post/7607">TypeError: can't convert 'str' object to str implicitly</a>:</p>
<blockquote>
<p dir="auto">s = b'foo|bar'</p>
</blockquote>
<p dir="auto">The variable s that you create is of type byte, while split() is intended to be used on variables of type str (string).<br />
The other two code fragments that you provide first convert the byte to a string before split() is applied.<br />
Take a look at this:</p>
<pre><code>&gt;&gt;&gt; s = b'foo|bar'
&gt;&gt;&gt; type(s)
&lt;class 'bytes'&gt;
&gt;&gt;&gt; s2 = str(s)
&gt;&gt;&gt; type(s2)
&lt;class 'str'&gt;
&gt;&gt;&gt; s3= s.decode('utf-8')
&gt;&gt;&gt; type (s3)
&lt;class 'str'&gt;
</code></pre>
<p dir="auto">the type() function gives you the type of an object/variable. You can see that .decode() and str() causes the byte object to be converted explicitly to a string (instead of implicitly expecting that that is being done for you by using split() on a byte object).</p>
<p dir="auto">Hope this explains it?</p>
]]></description><link>https://forum.pycom.io/post/7646</link><guid isPermaLink="true">https://forum.pycom.io/post/7646</guid><dc:creator><![CDATA[PiAir]]></dc:creator><pubDate>Sat, 20 May 2017 15:28:13 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Sat, 20 May 2017 15:36:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1230">@Andrea-Filippini</a><br />
<code>sp = s.split(b'|')</code><br />
works too. The difference is, that in the error case you mix string and bytes objects. The conversion you apply in your working examples you show convert the bytes object to string, and the split with a string works.<br />
P.S.: I would say that the error message is misleading.</p>
]]></description><link>https://forum.pycom.io/post/7647</link><guid isPermaLink="true">https://forum.pycom.io/post/7647</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sat, 20 May 2017 15:36:27 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Sat, 20 May 2017 15:40:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> it works but gives a different result:</p>
<pre><code>&gt;&gt;&gt; sp = s.split(b'|')
&gt;&gt;&gt; type(sp)
&lt;class 'list'&gt;
</code></pre>
]]></description><link>https://forum.pycom.io/post/7648</link><guid isPermaLink="true">https://forum.pycom.io/post/7648</guid><dc:creator><![CDATA[PiAir]]></dc:creator><pubDate>Sat, 20 May 2017 15:40:16 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Sat, 20 May 2017 15:54:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1173">@PiAir</a> split() always returns a list. You'll see a difference with type(sp[0]).</p>
]]></description><link>https://forum.pycom.io/post/7649</link><guid isPermaLink="true">https://forum.pycom.io/post/7649</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sat, 20 May 2017 15:54:15 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Sat, 20 May 2017 15:58:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> my bad, you're right</p>
<pre><code>&gt;&gt;&gt; type(s.split(b'|')[0])
&lt;class 'bytes'&gt;
&gt;&gt;&gt; type(str(s).split('|')[0])
&lt;class 'str'&gt;
</code></pre>
]]></description><link>https://forum.pycom.io/post/7650</link><guid isPermaLink="true">https://forum.pycom.io/post/7650</guid><dc:creator><![CDATA[PiAir]]></dc:creator><pubDate>Sat, 20 May 2017 15:58:08 GMT</pubDate></item><item><title><![CDATA[Reply to TypeError: can&#x27;t convert &#x27;str&#x27; object to str implicitly on Tue, 17 Mar 2020 07:47:01 GMT]]></title><description><![CDATA[<p dir="auto">Python makes a clear distinction between bytes and strings . Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is explicit: you encode a string to get bytes, specifying an encoding (which defaults to UTF-8); and you decode bytes to get a string. Clients of these functions should be aware that such conversions may fail, and should consider how failures are handled.</p>
<p dir="auto">We can convert <a href="http://net-informations.com/python/iq/byte.htm" target="_blank" rel="noopener noreferrer nofollow">bytes to string</a> using bytes class decode() instance method, So you need to decode the bytes object to produce a string. In Python 3 , the default encoding is &quot;utf-8&quot; , so you can use directly:</p>
<pre><code>b&quot;python byte to string&quot;.decode(&quot;utf-8&quot;)
</code></pre>
]]></description><link>https://forum.pycom.io/post/32148</link><guid isPermaLink="true">https://forum.pycom.io/post/32148</guid><dc:creator><![CDATA[germyrinn]]></dc:creator><pubDate>Tue, 17 Mar 2020 07:47:01 GMT</pubDate></item></channel></rss>