I aim to convert a proper noun for instance to have an upper case first letter after an input of the name has been made.
Using string.title() you achieve that:
>>> name = 'joe'
>>> name.title()
'Joe'
Use upper() method, like this:
mystr = "hello world"
mystr = mystr[0].upper() + mystr[1:]
.capitalize() and .title(), can be used, but both have issues:
>>> "onE".capitalize()
'One'
>>> "onE".title()
'One'
Both changes other letters of the string to smallcase.
Write your own:
>>> xzy = lambda x: x[0].upper() + x[1:]
>>> xzy('onE')
'OnE'
You can use https://pydash.readthedocs.io/en/latest/api.html#pydash.strings.capitalize.
Install pydash - pip install pydash
example:
from pydash import py_
greetings = "hello Abdullah"
py_.capitalize(greetings) # returns 'Hello abdullah'
py_.capitalize(greetings, strict = False) # returns 'Hello Abdullah'
Related
How can I replace word with "word" in python?
I've already tried this:
str = "some_word"
str.replace("some_word", '"some_word"')
but this doesn't work
1) Don't use str as variable name as it is keyword in python.
d = 'ab'
print(d.replace('ab','ac'))
2) String are immutable in python so you have to re-assign it like:
d = d.replace('ab','ac')
print(d)
you should create a variable to receive the replaced value.
str = "some_word"
str = replace(old_chars, new_chars)
>>> strr = "some_word"
>>> strr = f'"{strr}"'
>>> print(strr)
"some_word"
This perfectly works:
a = "some_word"
print(a) # some_word
a = a.replace("some_word", '"some_word"')
print(a) # "some_word"
If you want to wrap the entire string in double quotes, and not only a part of it, you may use one of these approaches:
a = "some_word"
a1 = '"{}"'.format(a)
a2 = '"' + a + '"'
a3 = f'"{a}"'
I have a string in which I want to replace some variables, but in different steps, something like:
my_string = 'text_with_{var_1}_to_variables_{var_2}'
my_string.format(var_1='10')
### make process 1
my_string.format(var_2='22')
But when I try to replace the first variable I get an Error:
KeyError: 'var_2'
How can I accomplish this?
Edit:
I want to create a new list:
name = 'Luis'
ids = ['12344','553454','dadada']
def create_list(name,ids):
my_string = 'text_with_{var_1}_to_variables_{var_2}'.replace('{var_1}',name)
return [my_string.replace('{var_2}',_id) for _id in ids ]
this is the desired output:
['text_with_Luis_to_variables_12344',
'text_with_Luis_to_variables_553454',
'text_with_Luis_to_variables_dadada']
But using .format instead of .replace.
In simple words, you can not replace few arguments with format {var_1}, var_2 in string(not all) using format. Even though I am not sure why you want to only replace partial string, but there are few approaches that you may follow as a workaround:
Approach 1: Replacing the variable you want to replace at second step by {{}} instead of {}. For example: Replace {var_2} by {{var_2}}
>>> my_string = 'text_with_{var_1}_to_variables_{{var_2}}'
>>> my_string = my_string.format(var_1='VAR_1')
>>> my_string
'text_with_VAR_1_to_variables_{var_2}'
>>> my_string = my_string.format(var_2='VAR_2')
>>> my_string
'text_with_VAR_1_to_variables_VAR_2'
Approach 2: Replace once using format and another using %.
>>> my_string = 'text_with_{var_1}_to_variables_%(var_2)s'
# Replace first variable
>>> my_string = my_string.format(var_1='VAR_1')
>>> my_string
'text_with_VAR_1_to_variables_%(var_2)s'
# Replace second variable
>>> my_string = my_string % {'var_2': 'VAR_2'}
>>> my_string
'text_with_VAR_1_to_variables_VAR_2'
Approach 3: Adding the args to a dict and unpack it once required.
>>> my_string = 'text_with_{var_1}_to_variables_{var_2}'
>>> my_args = {}
# Assign value of `var_1`
>>> my_args['var_1'] = 'VAR_1'
# Assign value of `var_2`
>>> my_args['var_2'] = 'VAR_2'
>>> my_string.format(**my_args)
'text_with_VAR_1_to_variables_VAR_2'
Use the one which satisfies your requirement. :)
Do you have to use format? If not, can you just use string.replace? like
my_string = 'text_with_#var_1#_to_variables_#var2#'
my_string = my_string.replace("#var_1#", '10')
###
my_string = my_string.replace("#var2#", '22')
following seems to work now.
s = 'a {} {{}}'.format('b')
print(s) # prints a b {}
print(s.format('c')) # prints a b c
I'm wondering what is the Python way to perform the following -
Given a set :
s = {'s1','s2','s3'}
I would like to perform something like :
s.addToAll('!')
to get
{'s1!','s2!','s3!'}
Thanks!
For an actual set:
>>> s = {'s1','s2','s3'}
>>> {x + '!' for x in s}
set(['s1!', 's2!', 's3!'])
That method is 2.7+, If you are using Python 2.6 you would have to do this instead:
>>> s = set(['s1','s2','s3'])
>>> set(x + '!' for x in s)
set(['s1!', 's2!', 's3!'])
You can try this:
>>> s = ['s1','s2','s3']
>>> list(i + '!' for i in s)
is there a quick way to place a string in the front of another string in python? if so how?
as an example let's say that string = 'pple'. How would I put the string_2 = 'a' at the start of string?
concatenate it:
string=char+string
>>> strg = 'pple'
>>> char = 'a'
>>> char + strg
'apple'
>>> strg = char + strg
>>> strg
'apple'
>>>
Here's a quick example:
string='a'+string
Lots of correct answers. You could also use "string interpolation" (or just "string formatting", when referring to str.format) if you are really looking to do some string manipulating (I only mention it because figuring out what the % is called can be frustrating):
>>> one_string = 'One string'
>>> two_string = 'two string'
>>> one_two = '%s, %s, red string, blue string' % (one_string, two_string)
>>> one_two
'One string, two string, red string, blue string'
I'll leave you to check it out if you like. See, for example, http://docs.python.org/library/stdtypes.html#string-formatting-operations
>>> word = '\u041a\u041e\u041b'
>>> print u'\u041a\u041e\u041b'
КОЛ
>>> print word
\u041a\u041e\u041b
How to transform string as a variable to a
readable kind (how print word)?
>>> print '\u041a\u041e\u041b'.decode('unicode-escape')
КОЛ