{"id":414,"date":"2022-01-28T16:14:27","date_gmt":"2022-01-28T15:14:27","guid":{"rendered":"https:\/\/alessandromasciadri.com\/?p=414"},"modified":"2022-01-31T21:46:49","modified_gmt":"2022-01-31T20:46:49","slug":"come-rimuovere-il-primo-carattere-da-una-stringa-in-python","status":"publish","type":"post","link":"https:\/\/alessandromasciadri.com\/come-rimuovere-il-primo-carattere-da-una-stringa-in-python\/","title":{"rendered":"Come rimuovere il primo carattere da una stringa in Python"},"content":{"rendered":"\t\t<div data-akihiro-type=\"ama-post\" data-akihiro-id=\"414\" class=\"akihiro akihiro-414\" data-akihiro-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"akihiro-section akihiro-top-section akihiro-element akihiro-element-338e9e8 akihiro-section-boxed akihiro-section-height-default akihiro-section-height-default\" data-id=\"338e9e8\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"akihiro-container akihiro-column-gap-default\">\n\t\t\t\t\t<div class=\"akihiro-column akihiro-col-100 akihiro-top-column akihiro-element akihiro-element-aaf7273\" data-id=\"aaf7273\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"akihiro-widget-wrap akihiro-element-populated\">\n\t\t\t\t\t\t<div class=\"akihiro-element akihiro-element-27dfc57 akihiro-widget akihiro-widget-text-editor\" data-id=\"27dfc57\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>In questo articolo scopriamo diversi metodi su come rimuovere il primo carattere da una stringa utilizzando Python. Ricordiamo per\u00f2 che in Python una stringa \u00e8 immutabile, pertanto se vogliamo rimuovere un carattere da una stringa dobbiamo costruire una stringa nuova che conterr\u00e0 la stringa precedente privata del primo carattere.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-8a369c5 akihiro-widget akihiro-widget-heading\" data-id=\"8a369c5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h2 class=\"akihiro-heading-title akihiro-size-default\">Rimuovere il primo carattere usando lo slicing<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-4f715de akihiro-widget akihiro-widget-text-editor\" data-id=\"4f715de\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Se vogliamo rimuovere il primo carattere o specifici caratteri da una stringa, possiamo ricorrere allo slicing.<\/p><p>Se per esempio volessimo rimuovere il primo carattere dalla stringa <strong>hhello<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-749b065 akihiro-widget akihiro-widget-code-highlight\" data-id=\"749b065\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>string = \"hhello\"\r\nnew_string = string[1:]\r\nprint(new_string)<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-b879655 akihiro-widget akihiro-widget-text-editor\" data-id=\"b879655\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Output<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-fcd72dd akihiro-widget akihiro-widget-code-highlight\" data-id=\"fcd72dd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash \">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>hello world<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-9ba9702 akihiro-widget akihiro-widget-heading\" data-id=\"9ba9702\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h2 class=\"akihiro-heading-title akihiro-size-default\">Rimuovere i primi caratteri usando il metodo lstrip()<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-bf4ca86 akihiro-widget akihiro-widget-text-editor\" data-id=\"bf4ca86\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Il metodo str.lstrip() riceve come parametro di input uno o pi\u00f9 caratteri e li rimuove dall&#8217;inizio della stringa restituendo una nuova stringa privata dei caratteri indicati.<\/p><p>Poniamo per\u00f2 attenzione al fatto che se i caratteri cercati sono presenti pi\u00f9 volte all&#8217;inizio della stringa, il metodo str.lstrip() li rimuove tutti.<\/p><p>Il seguente esempio chiarifica meglio quanto detto sopra.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-6ca6bbc akihiro-widget akihiro-widget-code-highlight\" data-id=\"6ca6bbc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>string = \"Hhello world\"\r\nnew_string = string.lstrip(\"H\")\r\nprint(new_string)\r\n\r\nstring = \"HHHHhello world\"\r\nnew_string = string.lstrip(\"H\")\r\nprint(new_string)<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-b284067 akihiro-widget akihiro-widget-text-editor\" data-id=\"b284067\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Output<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-89a5417 akihiro-widget akihiro-widget-code-highlight\" data-id=\"89a5417\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash \">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>hello world\r\nhello world<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-d045a5b akihiro-widget akihiro-widget-heading\" data-id=\"d045a5b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h2 class=\"akihiro-heading-title akihiro-size-default\">Rimuovere il primo carattere usando <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-bee11ed akihiro-widget akihiro-widget-text-editor\" data-id=\"bee11ed\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Il metodo re.sub() della libreria re pu\u00f2 essere un valido aiuto per rimuovere il primo carattere da una stringa.<\/p><p>re.sub() rimpiazza tutti i carattere che fanno match con l&#8217;espressione regolare indicata come primo argomento con il carattere indicato come secondo argomento.<\/p><p>Il seguente esempio aiuta a chiarire il concetto.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-0de9487 akihiro-widget akihiro-widget-code-highlight\" data-id=\"0de9487\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>import re\r\n\r\nstring = \"Hhello world\"\r\nnew_string = re.sub(r'.', '', string, count = 1)\r\nprint(new_string)<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-c1d439b akihiro-widget akihiro-widget-text-editor\" data-id=\"c1d439b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Output<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-3006f33 akihiro-widget akihiro-widget-code-highlight\" data-id=\"3006f33\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash \">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>hello world<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>In questo articolo scopriamo diversi metodi su come rimuovere il primo carattere da una stringa utilizzando Python. Ricordiamo per\u00f2 che in Python una stringa \u00e8 immutabile, pertanto se vogliamo rimuovere un carattere da una stringa dobbiamo costruire una stringa nuova che conterr\u00e0 la stringa precedente privata del primo carattere. Rimuovere il primo carattere usando lo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[12],"class_list":["post-414","post","type-post","status-publish","format-standard","hentry","category-programmazione","tag-python"],"_links":{"self":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":4,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/414\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}