38 More On String Methods | Python [ COMPLETE COURSE ]

Publicado el: 19 agosto 2020
en el canal de: Tech Lab
14
0

We've covered a bunch of String class methods already, so let's keep building on those and run down some more advanced methods.

The string method lower will return the string with all characters changed to lowercase. The inverse of this is the upper method, which will return the string all in uppercase. Just like with previous methods, we call these on a string using dot notation, like "this is a string".upper(). This would return the string "THIS IS A STRING". This can be super handy when checking user input, since someone might type in all lowercase, all uppercase, or even a mixture of cases.

You can use the strip method to remove surrounding whitespace from a string. Whitespace includes spaces, tabs, and newline characters. You can also use the methods lstrip and rstrip to remove whitespace only from the left or the right side of the string, respectively.

The method count can be used to return the number of times a substring appears in a string. This can be handy for finding out how many characters appear in a string, or counting the number of times a certain word appears in a sentence or paragraph.

If you wanted to check if a string ends with a given substring, you can use the method endswith. This will return True if the substring is found at the end of the string, and False if not.

The isnumeric method can check if a string is composed of only numbers. If the string contains only numbers, this method will return True. We can use this to check if a string contains numbers before passing the string to the int() function to convert it to an integer, avoiding an error.

Christine Rafla
Email us at - techlaboffice77@gmail.com


En esta página del sitio puede ver el video en línea 38 More On String Methods | Python [ COMPLETE COURSE ] de Duración hora minuto segunda en buena calidad , que subió el usuario Tech Lab 19 agosto 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 14 veces y le gustó 0 a los espectadores. Disfruta viendo!