EASY How do I use the count() and capitalize() function? [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I am new to Python and stack overflow. How do I use the count and capitalize functions for my strings? The app I am learning from appears to have this mixed up because it dosen't work in vscode.
It seems like the capitalize function call is ignored. What am I doing wrong? If someone could tell me how to use the count function as well I have the same problem. The app I learn from is called Programminz.
This is Python 3
practice = "CaPITalIzE mE proPerLy"
practice.capitalize()
print(practice)

string_name.capitalize()
string_name: It is the name of string of
whose first character we want
to capitalize.
Try to use :
string = "CaPITalIzE mE proPerLy"
capitalized_string = string.capitalize()
print('Old String: ', string)
print('Capitalized String:', capitalized_string)

Related

How to call dynamicly string operations in Python? [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed last month.
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'

Function requires a path. How do I make it into a raw path? [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!

Detect what a python string begins with [duplicate]

This question already has answers here:
Checking whether a string starts with XXXX
(5 answers)
Closed 5 years ago.
To detect whether a python string ends with a particular substring, say ".txt", there is a convenient built-in python string method;
if file_string.endswith(".txt"):
I would like to to detect whether a python string begins with a particular substring, say "begin_like_this". I am not able find a convenient method that I can use like this;
if file_string.beginswith("begin_like_this"):
I am using Python 3.6.
You're looking for str.startswith
if file_string.startswith("begin_like_this"):

Ruby's "next" implementation in Python [duplicate]

This question already has answers here:
How can I get the next string, in alphanumeric ordering, in Python?
(4 answers)
Closed 6 years ago.
If Python has an implementation of Ruby's next method? I mean something what works exactly the same as in Ruby, so if I type e.g. "z".next it will return "aa" (instead of just next sign in ascii table), "az".next will return "ba" and so on.
I don't believe there is a built-in method for this in Python. A similar question was asked on How can I get the next string, in alphanumeric ordering, in Python? and the accepted answer gives a solution.

Count letters in string python [duplicate]

This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi

Categories