简体   繁体   English

我无法在 Python 中将字符串对象转换为字符串

[英]I can't convert a string object to string in Python

I am facing a problem that I found in a study book on Python.我正面临一个在 Python 学习书中发现的问题。

I'll preface this by saying that I haven't studied "classes" yet (I haven't gotten to that chapter yet) and my training is mainly based on loops, functions, lists, arrays, Series and DataFrame, regular expressions.我先说我还没有研究过“类”(我还没有读到那一章),我的训练主要基于循环、函数、列表、数组、Series 和 DataFrame、正则表达式。

In the chapter on string handling, I was asked to perform an exercise that randomly fishes words out of 4 different arrays and joins them into a single string, finally changing the first letter of the sentence (putting it in uppercase) and adding a period (.) at the end of the sentence.在关于字符串处理的章节中,我被要求做一个练习,从 4 个不同的数组中随机取出单词并将它们连接成一个字符串,最后更改句子的第一个字母(将其变为大写)并添加一个句点( .) 在句末。

Based on the code below, I was only unable to complete the uppercase character and the period.根据下面的代码,我只能完成大写字符和句号。

It would appear that Python does not see the object named "frase" (an italian word which means sentence in english) as a single string, but only as an object composed of a set of characters separated from each other.看起来 Python 不会将名为“frase”(意大利语单词,意思是英语中的句子)的对象视为单个字符串,而只会将其视为由一组相互分隔的字符组成的对象。

How do I convert the "phrase" object to a unique string without using the classes I have not yet studied?如何在不使用我尚未学习的类的情况下将“短语”对象转换为唯一字符串?

I tell you this since I have only found methods on the Internet that involve the use of classes.我告诉你这个是因为我在 Internet 上只找到涉及使用类的方法。

Thank you in advance for any support you can lend me.预先感谢您提供给我的任何支持。

A very happy programming to everyone给大家一个非常开心的编程

import random
import numpy as np

frase = ''

articolo = np.array(['il',
                     'lo',
                     'la',
                     'gli',
                     'le',
                     'un',
                     'uno',
                     'una'])
nome = np.array(['fagotto', 
                 'coccinella', 
                 'puntale', 
                 'piastrella', 
                 'serra', 
                 'magia',
                'miope', 
                 'curvo'])
verbo = np.array(['amare', 
                  'tornare', 
                  'sapere', 
                  'piacere', 
                  'dare', 
                  'fare', 
                  'essere',
                 'leggere'])
preposizione = np.array(['di', 
                         'a', 
                         'da', 
                         'in', 
                         'con', 
                         'su', 
                         'per', 
                         'tra'])

def casuale(arr):
    return arr[random.randrange(8)]

for i in range(20):
    frase = ''
    for j in [articolo, nome, verbo, preposizione, articolo, nome]:
        frase += casuale(j) + ' '
    frase.strip()
    frase += '.'
    frase.capitalize()
    print(f"{frase}\n")

I tried converting the object with the STR() function but it did not work.我尝试使用 STR() 函数转换对象,但没有成功。

Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter, while making all other characters in the string lowercase letters. Python String capitalize() 方法返回原始字符串的副本并将字符串的第一个字符转换为大写(大写)字母,同时将字符串中的所有其他字符转换为小写字母。

Key word here is "Returns".这里的关键词是“回报”。 You're using capitalize method (a sort of special function, it will become clear in the class paragraph) but you're not using the value it returns.您正在使用 capitalize 方法(一种特殊函数,它会在类段落中变得清晰)但您没有使用它返回的值。

frase = frase.capitalize()
print(f"{frase}\n")

I've re-written your code to improve the structure and layout.我已经重写了您的代码以改进结构和布局。 Note - use Lists not numpy arrays for text.注意 - 使用列表而不是 numpy 数组作为文本。 Note use of random.choice.注意使用random.choice.

import random

articolo = ['il', 'lo', 'la', 'gli', 'le', 'un', 'uno', 'una']
nome = ['fagotto', 'coccinella', 'puntale', 'piastrella', 'serra', 'magia', 'miope', 'curvo']
verbo = ['amare', 'tornare', 'sapere', 'piacere', 'dare', 'fare', 'essere', 'leggere']
preposizione = ['di', 'a', 'da', 'in', 'con', 'su', 'per', 'tra']

for i in range(20):
    frase = ''
    for j in [articolo, nome, verbo, preposizione, articolo, nome]:
        parola = random.choice(j)
        frase += parola + ' '
    frase += '.'
    frase = frase.capitalize()
    print(f"{frase}\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM