How to add a string in a certain position?
No. Python Strings are immutable.
>>> s='355879ACB6'
>>> s[4:4] = '-'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
It is, however, possible to create a new string that has the inserted character:
>>> s[:4] + '-' + s[4:]
'3558-79ACB6'
Inserting string at position x of another string
var a = "I want apple";var b = " an";var position = 6;var output = [a.slice(0, position), b, a.slice(position)].join('');console.log(output);
Insert a string at a specific index
You could prototype your own splice()
into String.
Polyfill
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
Example
String.prototype.splice = function(idx, rem, str) { return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));};
var result = "foo baz".splice(4, 0, "bar ");
document.body.innerHTML = result; // "foo bar baz"
Add string in a certain position in column in dataframe
You can perform the operation you presented on a list but you have a column in a dataframe so its (a bit) different.
So while you can do this:
hash = "355879ACB6"
hash = hash[:4] + '-' + hash[4:]
in order to do this on a dataframe you can do it in at least 2 ways:
consider this dummy df:
LOCATION Hash
0 USA 355879ACB6
1 USA 455879ACB6
2 USA 388879ACB6
3 USA 800879ACB6
4 JAPAN 355870BCB6
5 JAPAN 355079ACB6
A. vectorization: the most efficient way
df['new_hash']=df['Hash'].str[:4]+'-'+df['Hash'].str[4:]
LOCATION Hash new_hash
0 USA 355879ACB6 3558-79ACB6
1 USA 455879ACB6 4558-79ACB6
2 USA 388879ACB6 3888-79ACB6
3 USA 800879ACB6 8008-79ACB6
4 JAPAN 355870BCB6 3558-70BCB6
5 JAPAN 355079ACB6 3550-79ACB6
B. apply lambda: intuitive to implement but less attractive in terms of performance
df['new_hash'] = df.apply(lambda x: x['Hash'][:4]+'-'+x['Hash'][4:], axis=1)
How to insert a character in a string at a certain position?
int j = 123456;
String x = Integer.toString(j);
x = x.substring(0, 4) + "." + x.substring(4, x.length());
Insert value into a string at a certain position?
You can't modify strings; they're immutable. You can do this instead:
txtBox.Text = txtBox.Text.Substring(0, i) + "TEXT" + txtBox.Text.Substring(i);
Insert into a certain position in a string
This should work:
string s = "MOT1-G-PRT45-100";
int index = ... // position to insert
string res = s.Insert(index, c + separator);
Append to String at certain position
Use the insert method.
info.insert(19, Misc.toTimestampLiteral(currentDate));
Related Topics
Accessing Mp3 Metadata with Python
How to Dynamically Compose an or Query Filter in Django
Problem with Multi Threaded Python App and Socket Connections
How to Format a String Using a Dictionary in Python-3.X
Convert a List of Characters into a String
Is a Python Dictionary an Example of a Hash Table
Splitting a List Based on a Delimiter Word
Python: Urlerror: <Urlopen Error [Errno 10060]
Cannot Concatenate 'Str' and 'Float' Objects
Split a Python List into Other "Sublists" I.E Smaller Lists
How to Unzip a List of Tuples into Individual Lists
How to Scroll Frame Using Mouse Wheel & Adding Horizontal Scrollbar
Oserror [Errno 22] Invalid Argument When Use Open() in Python
Decorating Class Methods - How to Pass the Instance to the Decorator